Subdividing column matrix based on two stored indices of different lengths
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have a column matrix 'A' that could be any length with any value and a coresponding column matrix of same length with whole number values within. I would like to pull values from 'A' into a new column matrix 'B' based on a column matrix of any length 'pull' where the values I pull correspond to the number in 'order rather an the row indices. I recognize this may be a confusing explanation so see the indended output in the coding section. What is a good way to do this? I can see how looping and checks could work but I would like to avoid loops if possible.
A = [0.0048;-0.001;-0.0042;0.0047;-0.00011;0.0025;0;0;0;0;0.0023;0.0004];
order =[4;5;6;7;8;9;1;2;3;10;11;12];
pull = [7;8;9;10;11;12];
%intended output
B = [0.0047;-0.00011;0.0025;0;0.0023;0.0004];
alternative values for 'pull' are shown below.
pull = [1;2;3;4;5;6];
pull = [1;7];
0 comentarios
Respuestas (1)
dpb
el 21 de Abr. de 2023
Editada: dpb
el 21 de Abr. de 2023
A = [0.0048;-0.001;-0.0042;0.0047;-0.00011;0.0025;0;0;0;0;0.0023;0.0004];
order =[4;5;6;7;8;9;1;2;3;10;11;12];
pull = [7;8;9;10;11;12];
[~,ib]=ismember(pull,order);
B=A(ib)
I think your example B above is in error for the last three locations...they're the last three locations in the order array.
5 comentarios
dpb
el 21 de Abr. de 2023
Editada: dpb
el 21 de Abr. de 2023
OK, if order is important, then use (updated Answer above as well; tried to avoid the temporary ib as order wasn't specifically required in original...although I suppose it could have been inferred from the answers proposed).
A = [0.0048;-0.001;-0.0042;0.0047;-0.00011;0.0025;0;0;0;0;0.0023;0.0004];
order =[4;5;6;7;8;9;1;2;3;10;11;12];
pull = [[1;2;3;4;5;6] [7;8;9;10;11;12]];
for i=1:size(pull,2)
[~,ib]=ismember(pull(:,i),order);
B(:,i)=A(ib);
end
B.'
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!