selecting rows from C with associated values in D, while using A and B values as references to produce E and F matrices

1 vue (au cours des 30 derniers jours)
Hi everyone, I have 2 matrices, A and B, that I want to use as reference to draw out certain rows in C and attached associated values from D.
A = [1 2];
B = [1 3];
C = [ 1 19 18 4 3 0 0 0 0 0 0
1 19 2 17 6 7 5 4 3 2 0
1 16 15 17 6 7 5 4 3 2 0
1 19 2 3 0 0 0 0 0 0 0
1 16 15 14 9 8 7 5 4 3 2
1 16 15 14 9 6 5 4 3 2 0
1 2 3 0 0 0 0 0 0 0 0
];
D = [0.1
0.3
0.4
0.8
0.9
0.11
0.2
];
my end results for E and F should be:
E = [0.1 1 19 18 4 3 0 0 0 0 0 0
0.8 1 19 2 3 0 0 0 0 0 0 0
0.2 1 2 3 0 0 0 0 0 0 0 0
];
F = [0.3 1 19 2 17 6 7 5 4 3 2 0
0.4 1 16 15 17 6 7 5 4 3 2 0
0.9 1 16 15 14 9 8 7 5 4 3 2
0.11 1 16 15 14 9 6 5 4 3 2 0
];
  2 commentaires
madhan ravi
madhan ravi le 14 Sep 2019
Difficult to understand , illustrate with a short example.
JL
JL le 14 Sep 2019
Modifié(e) : JL le 14 Sep 2019
Hi Madhan, so basically, put them on E and F based on their first and last number (ignore zero in each rows). For examples, for [1 3], I select the rows in C to put in E with a row starting with 1 and ending with 3 (like I said, ignore zeros)

Connectez-vous pour commenter.

Réponse acceptée

Bruno Luong
Bruno Luong le 14 Sep 2019
Modifié(e) : Bruno Luong le 14 Sep 2019
A = [1 2];
B = [1 3];
C = [ 1 19 18 4 3 0 0 0 0 0 0;
1 19 2 17 6 7 5 4 3 2 0;
1 16 15 17 6 7 5 4 3 2 0;
1 19 2 3 0 0 0 0 0 0 0;
1 16 15 14 9 8 7 5 4 3 2;
1 16 15 14 9 6 5 4 3 2 0;
1 2 3 0 0 0 0 0 0 0 0
];
D = [0.1;
0.3;
0.4;
0.8;
0.9;
0.11;
0.2
];
[i,j] = find(C);
getC = @(pickfun) C(sub2ind(size(C),(1:size(C,1))',accumarray(i(:),j(:),[],pickfun)));
filtFun = @(x) ismember([getC(@min),getC(@max)],x,'rows');
b = filtFun(B);
E = [D(b,:),C(b,:)]
b = filtFun(A);
F = [D(b,:),C(b,:)]

Plus de réponses (1)

per isakson
per isakson le 14 Sep 2019
Modifié(e) : per isakson le 15 Sep 2019
The script
%%
ixe = [1,4,7];
E = cat( 2, D(ixe), C(ixe,:) );
ixf = setdiff( [1:7], ixe );
F = cat( 2, D(ixf), C(ixf,:) );
calculates E and F that agrees with your sample.
However, I cannot see how the rows 1, 4 and 7, honors the rules you provide in your comment.
In response to comment
ixe = find_rows( C, B );
E = cat( 2, D(ixe), C(ixe,:) );
ixf = find_rows( C, A );
F = cat( 2, D(ixf), C(ixf,:) );
%
function ix_rows = find_rows( C, FL );
ix_rows = nan( 1, size(C,1) );
for jj = 1 : size( C, 1 )
row = C(jj,:);
row(row==0) = [];
if row(1)==FL(1) && row(end)==FL(2)
ix_rows(jj) = jj;
end
end
ix_rows( isnan( ix_rows ) ) = [];
end
  1 commentaire
JL
JL le 14 Sep 2019
Hi isakson, can it select automatically rather than selecting 1 4 7 to put to E then putting the rest in F? The reason being, I have 3000 over rows and they are not just for E and F.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by