Hi all, i am new and i have one question. I am trying to copy and paste values for one column of one array to other array with different size. I paste some pictures and code for my explanation:
The array A has two columns and i want copy the values of column two.
Then, i want to copy value of column 1 of A that match with column 1 of B and if match this values, copy values of column two of A in column two of B.
The result would be :
I had code but with loop. Is there some way with indexation?
Thank you very much and sorry for my poor explanation.
if true
% code
A=[1 12; 2 14; 3 18; 4 25; 5 32];
B=[1;1;1;2;2;3;3;3;4;4;5;5;5;5];
n = length(A); m = length(B);
for i = 1:m; for j = 1:n; if B(i,1) == A(j,1); B(i,2) = A(j,2); end end end
end

 Respuesta aceptada

Star Strider
Star Strider el 26 de Jul. de 2018

0 votos

You only need to use simple indexing:
A=[1 12; 2 14; 3 18; 4 25; 5 32];
B=[1;1;1;2;2;3;3;3;4;4;5;5;5;5];
B = [B A(B,2)]
B =
1 12
1 12
1 12
2 14
2 14
3 18
3 18
3 18
4 25
4 25
5 32
5 32
5 32
5 32

Más respuestas (0)

Categorías

Productos

Versión

R2008b

Preguntada:

el 26 de Jul. de 2018

Respondida:

el 26 de Jul. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by