How to put value from one matrix into another matrix
27 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have two matrixes called A and B. The matrix A is 20x2 and matrix B is 4x5. I suppose the first column of matrix A is 1 to 20 and the second column is the values of number 1-20. The matrix B is a new rearraged matrix of the first column of matrix A (number 1-20). I want to put the values from the second column of matrix A into the matrix B.
I have search the Matlab function that maybe related is ismember. But I am still confused how to coding. Thank you in advance
0 commentaires
Réponses (3)
Torsten
le 28 Fév 2023
Modifié(e) : Torsten
le 28 Fév 2023
A = [3:42;rand(1,40)].';
B = [6 45 18 24 33;5 12 17 23 32;4 11 16 22 31;3 10 15 21 30];
result = zeros(size(B));
for i = 1:size(B,1)
for j = 1:size(B,2)
index = find(A(:,1)==B(i,j));
if isempty(index)
result(i,j) = NaN;
else
result(i,j) = A(index,2);
end
end
end
result
Stephen23
le 1 Mar 2023
A = [3:42;rand(1,40)].'
B = [6,45,18,24,33;5,12,17,23,32;4,11,16,22,31;3,10,15,21,30]
R = nan(size(B));
[X,Y] = ismember(B,A(:,1));
R(X) = A(Y(X),2)
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!