how to get the output of an if statement in a matrix
1 view (last 30 days)
Show older comments
Hello-- I have a question and it is very important to me. I will appreciate if anyone could help me on this issue! WE have two matrices: one is r(40*80) and the other one is s(8*1) Also we know that 8 rows of matrix r are zeros. ( for example : rows number 6,9,19,23,25,32,33,38 )
I need a code to just write the numbers of zero rows ( which are listed above ) in the s matrix in the order. I wrote this:
s=zeros(8,1);
for row_number = 1:40
A=r(row_number,:);
[i,v,c]=find(A);
row_number;
max(v);
min(v);
c;
if isempty(max(v))
for h=1:8
s(h,1)=row_number;
hold on
end
s
end
and this is the answer I got!
s =
6
6
6
6
6
6
6
6
s =
9
9
9
9
9
9
9
9
s =
19
19
19
19
19
19
19
19
s =
23
23
23
23
23
23
23
23
s =
25
25
25
25
25
25
25
25
s =
32
32
32
32
32
32
32
32
s =
33
33
33
33
33
33
33
33
s =
38
38
38
38
38
38
38
38
What I need to have is simply an answer with this form : s =
6
9
19
23
25
32
33
38
the reason I use find(A) was to do some calculations with min(v) and max(v) for rows with non zero elements. I will be thankful of anyone helping me with this. Best, Homayoon
0 Comments
Accepted Answer
Guillaume
on 19 Feb 2015
I have no idea what you're trying to do with your code (in particular why is there a hold on which is to do with graphics?), but the answer is simply:
s = find(all(r == 0, 2));
e.g:
r = randi([0 10], 40, 80); %demo data
r([6,9,19,23,25,32,33,38], :) = 0; %fill rows 6,9, etc. with 0
s = find(all(r == 0, 2))
outputs:
s =
6
9
19
23
25
32
33
38
More Answers (0)
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!