How to put value from one matrix into another matrix
32 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (3 件)
Torsten
2023 年 2 月 28 日
編集済み: Torsten
2023 年 2 月 28 日
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
2023 年 3 月 1 日
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)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!