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

8 views (last 30 days)
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
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.
Damith
Damith on 17 Mar 2014
Edited: Damith on 17 Mar 2014
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)

Mischa Kim
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
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
Damith on 18 Mar 2014
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 MATLAB 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!