How to swap array element from two arrays conditionally
5 ビュー (過去 30 日間)
古いコメントを表示
I'm looking to plot a matrix using data from two arrays. If the second array has a number which is not zero in an element, this element is moved into the first array. I have seen how this might be done using deal(), but I would like to avoid using a loop with if statement for performance reasons. Is there a convenient way of doing this?
An example of this is follows, with arrays A, B and C ( the result);
![Array.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/252918/Array.png)
0 件のコメント
採用された回答
Akira Agata
2019 年 12 月 4 日
By using indexing technique, you can simply do this kind of task.
The following is an example:
% Original matrixes
A = [...
0 1 1 1 0 0;...
0 0 0 0 0 0;...
0 0 0 0 0 0];
B = [...
0 3 0 0 4 0;...
0 2 0 0 2 0;...
0 0 0 0 0 0];
% Creating C by using index
idx = B ~= 0;
C = A;
C(idx) = B(idx);
その他の回答 (1 件)
JESUS DAVID ARIZA ROYETH
2019 年 12 月 4 日
C=A;
C(B~=0)=B(B~=0)
also could help you:
C=max(A,B)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!