# i am confused with commands for loop

26 views (last 30 days)
Hend Nasrallah on 8 Jun 2021
Commented: Rena Berman on 29 Jun 2021 at 14:33
Write MATLAB code including loop to look for first zero item in a randomly given vector (1 column and 10 rows). You need to include the generation of this random values vector in the code as well, the random values must be generated between 0 and 10. The search result should be associated with text: 'The zero is found in' and then state the element v(i,1) :
##### 2 CommentsShowHide 1 older comment
Rena Berman on 29 Jun 2021 at 14:33

Kunal Kandhari on 8 Jun 2021
To create an array of random numbers between range 0 to 10 with size (10,1):
arr=randi(11,1,10)-1;
% first parameter is range ie., 1-11
%second parameter is number of columns
% third parameter is number of rows
% We want random array between range 0-10 therefore we have substracted 1
% at end from all elements
For looping over array:
for i=1:length(arr)
end
For more detail about for loop you can refer:
Complete code for this question:
arr=randi(11,1,10)-1;
index=0;
%this index indicates that no 0's are found
disp(arr);
%print whole array, you can remove this line
for i=1:length(arr)
if(arr(i)==0)
%if array element is 0, store its position in index field and break
%loop
index=i;
break;
end
end
% if index is zero then no 0's are found and id index is not zero then 0 is
% present in array at position index
if(index==0)
else
disp("The zero is found in arr("+index+",1)");
end
Output screenshot: ##### 2 CommentsShowHide 1 older comment
James Tursa on 8 Jun 2021
@Kunal Kandhari We discourage posting complete answers to homework questions.

Sambit Supriya Dash on 8 Jun 2021
r = (randi([0 10],1,10))';
r0 = r == 0;
if sum(r0) == 0
else
for i = 1:length(r)
if r(i) == 0
fprintf('Zero is found in %.0f \n',i)
break
else
end
end
end
##### 2 CommentsShowHide 1 older comment
Sambit Supriya Dash on 9 Jun 2021
I'll take care to find further it's a homework question or genuine question some is interested to solve. Thank You