problem with an if statement

Dear all,
I have A =[
1 2 0 2 11 0;
2 3 0 30 11 -2;
3 4 0 28 12 -6;
4 5 0 25 1 -9;
5 6 0 22 2 -6;
6 7 0 22 3 -12;
7 8 0 19 4 -13;
8 9 0 17 5 -17;
9 10 0 14 6 -18;
10 11 0 12 7 -22;
11 12 0 9 8 -25;
12 13 0 6 9 -26;
13 14 0 4 10 -30;
14 15 0 1 11 -1;
15 16 0 29 11 -3;
16 17 0 27 12 0;
17 18 0 31 1 -3;
18 19 0 28 2 0;
19 20 0 28 3 -6;
20 21 0 25 4 -7;
21 22 0 23 5 -11;
22 23 0 20 6 -12;
23 24 0 18 7 -16;
24 25 0 15 8 -19;
25 26 0 12 9 -20;
26 27 0 10 10 -24;
27 28 0 7 11 -25;
28 29 0 5 12 -29;
29 30 0 2 1 -1;
30 31 0 30 1 -4;
31 32 0 27 2 -1;
32 33 0 27 3 -7;
33 34 0 24 4 -8;
34 35 0 22 5 -12;
35 36 0 19 6 -13;
36 37 0 17 7 -17;
37 38 0 14 8 -20;
38 39 0 11 9 -21;
39 39 0 9 10 -2]
I want to apply the following if statement: if the fourth column is 1 or 2 AND the fifth column contains identical successive numbers AND the element in the laste column in not zero then replace every other corresponding element in the third column with the corresponding element (in absolute value) from the last column
The if statement should select the following elements
B=[
14 15 0 1 11 -1;
15 16 0 29 11 -3;
29 30 0 2 1 -1;
30 31 0 30 1 -4;]
and then the action is to obtain
B=[
14 15 1 1 11 -1;
15 16 0 29 11 -3;
29 30 1 2 1 -1;
30 31 0 30 1 -4;]
So the new A should be
Anew =[
1 2 0 2 11 0;
2 3 0 30 11 -2;
3 4 0 28 12 -6;
4 5 0 25 1 -9;
5 6 0 22 2 -6;
6 7 0 22 3 -12;
7 8 0 19 4 -13;
8 9 0 17 5 -17;
9 10 0 14 6 -18;
10 11 0 12 7 -22;
11 12 0 9 8 -25;
12 13 0 6 9 -26;
13 14 0 4 10 -30;
14 15 1 1 11 -1;
15 16 0 29 11 -3;
16 17 0 27 12 0;
17 18 0 31 1 -3;
18 19 0 28 2 0;
19 20 0 28 3 -6;
20 21 0 25 4 -7;
21 22 0 23 5 -11;
22 23 0 20 6 -12;
23 24 0 18 7 -16;
24 25 0 15 8 -19;
25 26 0 12 9 -20;
26 27 0 10 10 -24;
27 28 0 7 11 -25;
28 29 0 5 12 -29;
29 30 1 2 1 -1;
30 31 0 30 1 -4;
31 32 0 27 2 -1;
32 33 0 27 3 -7;
33 34 0 24 4 -8;
34 35 0 22 5 -12;
35 36 0 19 6 -13;
36 37 0 17 7 -17;
37 38 0 14 8 -20;
38 39 0 11 9 -21;
39 39 0 9 10 -2]
Is there a clever coding for this?

1 commentaire

Conrad
Conrad le 1 Août 2012
If you look at your B matrix
B=[
14 15 0 1 11 -1;
15 16 0 29 11 -3;
29 30 0 2 1 -1;
30 31 0 30 1 -4;]
the rows
[15 16 0 29 11 -3]
and
[30 31 0 30 1 -4]
do not have a 1 or 2 in the fourth column, so how where they selected? According to the logic given, it does not make sense.

Connectez-vous pour commenter.

 Réponse acceptée

Conrad
Conrad le 1 Août 2012
Modifié(e) : Conrad le 1 Août 2012

0 votes

Okay, think I know what you mean... this should do it.
c5 = [A(2:end,5); NaN];
idx = ((A(:,4)==1)|(A(:,4)==2))&(A(:,5)==c5)&(A(:,6)~=0);
B = A(idx,:);
A(idx,3) = abs(A(idx,6));
Tested with your example and gives the correct value for A.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by