Check values using if condition for each element of two rows in a matrix

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

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.
Chris,
Sorry, My bad I did not explain the problem well. Let me elaborate.
I have a 3 matrices 365 rows x 40 columns of daily (365) precipitation data values for 40 years. So my matrices are AA = 365 x 40, BB = 365 x 40, CC = 365 x 40
I need to compare 2 rows at a time (e.g. row 2 with row 1, row 3 with row 2 across 40 years and so on)
Conditions need to check for each cell element of the first 2 rows across 40 years (e.g. row 2 and 1) are,
IF(AA(2,1)>0.1 and BB(2,1)=0,IF(CC(2,1)=0,IF(AA(1:1)=0,"1","0"))))
for row 3 and 2 are,
IF(AA(3,1)>0.1, BB(3,1)=0,IF(CC(3,1)=0,IF(AA(2:1)=0,"1","0"))))
. . .
upto row 365 and row 364. IF(AA(365,1)>0.1 and BB(365,1)=0,IF(CC(365,1)=0,IF(AA(364:1)=0,"1","0"))))
And I have 8 different set of conditions I need to check for same pairs of rows explained above. The eight conditions to check for each pair are as follows:
1)IF(AA(2,1)>0.1 and BB(2,1)=0 and IF(CC(2,1)=0 and IF(AA(1:1)=0,"1","0"))))
2)IF(AA(2,1)>0.1 and BB(2,1)=0 and IF(CC(2,1)=0 and IF(AA(1:1)>0.1,"1","0"))))
3)IF(AA(2,1)>0.1 and BB(2,1)=0 and IF(CC(2,1)>0.1 and IF(AA(1:1)=0,"1","0"))))
4)IF(AA(2,1)>0.1 and BB(2,1)=0 and IF(CC(2,1)>0.1 and IF(AA(1:1)>0.1,"1","0"))))
5)IF(AA(2,1)>0.1 and BB(2,1)>0.1 and IF(CC(2,1)=0 and IF(AA(1:1)=0,"1","0"))))
6)IF(AA(2,1)>0.1 and BB(2,1)>0.1 and IF(CC(2,1)=0 and IF(AA(1:1)>0.1,"1","0"))))
7)IF(AA(2,1)>0.1 and BB(2,1)>0.1 and IF(CC(2,1)>0.1 and IF(AA(1:1)=0,"1","0"))))
8)IF(AA(2,1)>0.1 and BB(2,1)>0.1 and IF(CC(2,1)>0.1 and IF(AA(1:1)>0.1,"1","0"))))
(NOTE: If all the conditions of each pair of rows are met, then output should be 1 otherwise zero)
Hope this helps to understand. My apologies for not explaining the problem well.
Hope to hear from you soon,
Thanks.

Sign in to comment.

Answers (1)

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

Mischa,
I really appreciate and thanks for your quick answer. Sorry, My bad I did not explain the problem well. Let me elaborate.
I have a 3 matrices 365 rows x 40 columns of daily (365) precipitation data values for 40 years. So my matrices are AA = 365 x 40, BB = 365 x 40, CC = 365 x 40
I need to compare 2 rows at a time (e.g. row 2 with row 1, row 3 with row 2 across 40 years and so on)
Conditions need to check for each cell element of the first 2 rows across 40 years (e.g. row 2 and 1) are,
IF(AA(2,1)>0.1 and BB(2,1)=0,IF(CC(2,1)=0,IF(AA(1:1)=0,"1","0"))))
for row 3 and 2 are,
IF(AA(3,1)>0.1, BB(3,1)=0,IF(CC(3,1)=0,IF(AA(2:1)=0,"1","0"))))
. . .
upto row 365 and row 364. IF(AA(365,1)>0.1 and BB(365,1)=0,IF(CC(365,1)=0,IF(AA(364:1)=0,"1","0"))))
And I have 8 different set of conditions I need to check for same pairs of rows explained above. The eight conditions to check for each pair are as follows:
1)IF(AA(2,1)>0.1 and BB(2,1)=0 and IF(CC(2,1)=0 and IF(AA(1:1)=0,"1","0"))))
2)IF(AA(2,1)>0.1 and BB(2,1)=0 and IF(CC(2,1)=0 and IF(AA(1:1)>0.1,"1","0"))))
3)IF(AA(2,1)>0.1 and BB(2,1)=0 and IF(CC(2,1)>0.1 and IF(AA(1:1)=0,"1","0"))))
4)IF(AA(2,1)>0.1 and BB(2,1)=0 and IF(CC(2,1)>0.1 and IF(AA(1:1)>0.1,"1","0"))))
5)IF(AA(2,1)>0.1 and BB(2,1)>0.1 and IF(CC(2,1)=0 and IF(AA(1:1)=0,"1","0"))))
6)IF(AA(2,1)>0.1 and BB(2,1)>0.1 and IF(CC(2,1)=0 and IF(AA(1:1)>0.1,"1","0"))))
7)IF(AA(2,1)>0.1 and BB(2,1)>0.1 and IF(CC(2,1)>0.1 and IF(AA(1:1)=0,"1","0"))))
8)IF(AA(2,1)>0.1 and BB(2,1)>0.1 and IF(CC(2,1)>0.1 and IF(AA(1:1)>0.1,"1","0"))))
(NOTE: If all the conditions of each pair of rows are met, then output should be 1 otherwise zero)
Hope this helps to understand. My apologies for not explaining the problem well.
Hope to hear from you soon,
Thanks.
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.
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.
Joseph,
Thanks. I think I made it clear that it should compares 2 rows at a time (e.g. row 2 and 1, 3 and 2, and so on) and should check the conditions for each pair of elements (cells) in selected pair of columns.
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.
Thanks. That would be a great help. Look forward to hear from you. Please let me know if you any questions.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 17 Mar 2014

Commented:

on 18 Mar 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!