Check values using if condition for each element of two rows in a matrix
Show older comments
Hi,
I need to check values of each element of rows (2,:) and (1,:), (3,:) and (2.:) and so on upto 8 rows. The matix size is 8 x 40 and the values have zeros and decimal values (10.5, 100.2, 154.2 etc)
How can I implement to check for set of conditions for pair of rows at a time changing row numbers?
For example, suppose my matrix is AA(8x40) IF(AA(2,1)>0.1,IF(AA(2,1)=0,IF(AA(2,1)=0,IF(AA(1:1)=0,"1","0"))))
Above is the MS Excel function.
How can I implement this in MATLAB?
Can anyone help,
Thanks.
2 Comments
Chris C
on 17 Mar 2014
I'm sorry. I really don't understand your questions. You want to check the rows of your matrix right? For what? That each element is the same? That one is larger than another.... there are nearly unlimited permutations of "checking" your matrix. If you could maybe try and explain the challenge a little more clearly, then I'd be happy to help.
Answers (1)
Mischa Kim
on 17 Mar 2014
Edited: Mischa Kim
on 17 Mar 2014
Damith, this is the basic structure:
data = rand(8,40);
for ii = 1:length(data(:,1))-1 % loop through all rows
if sum(data(ii,:)) > sum(data(ii+1,:)) % iith and (ii+1)st row
...
elseif *CONDITION*
...
else
...
end
end
6 Comments
Joseph Cheng
on 17 Mar 2014
I assume you know how to do each one of these in matlab, if so I would say break it up into each of these conditions and get a your 10 or so mxn results. At the end add them up and they should pass a threshold to have either satisfied all conditions or not.
Joseph Cheng
on 17 Mar 2014
Edited: Joseph Cheng
on 17 Mar 2014
If you do not know then we can break it down like this. example first condition you mentioned: IF(AA(2,1)>0.1 and BB(2,1)=0,IF(CC(2,1)=0,IF(AA(1:1)=0,"1","0")))) which is just the logical and of each condition (ie if all conditions met you get 1 else 0)
tempAA = AA>.1;
tempBB = BB==0;
tempCC = CC==0;
for the AA or one day earlier you can make a temporary AA that is shifted by one day. aa=[AA(1,:);AA(:,:)]; where the first day (row) would just be junk. then: tempaa = aa==0;
then you can just go
result = tempAA & tempBB & tempCC & tempaa;
to get your result.
Hope this is it because i'm not sure if i read your if statements correctly.
Damith
on 18 Mar 2014
Joseph Cheng
on 18 Mar 2014
Edited: Joseph Cheng
on 18 Mar 2014
I see how you are writing it now, not the best way to write it out. as a suggestion next time you are trying to reference 2 rows in a matlab setting would be AA(1:2,:)>0.1 instead of AA(2,1)>0.1. AA(1:2,:) saying rowas 1 to 2 for all columns. I thought you AA(2,1) was referencing row 2 column 1 of AA. Such that your first conditioning only was looking at the previous row in the most nested if statement of IF(AA(1:1). I'll think about it over night and see if i can get something by the morning.
Damith
on 18 Mar 2014
Categories
Find more on Loops and Conditional Statements 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!