Updating an array elements with elements from another Array

13 vues (au cours des 30 derniers jours)
Zaharaddeen Hussaini
Zaharaddeen Hussaini le 26 Mai 2018
Hello, Could I please get some assistance on how to update array elements with elements from another array, For example
A = rand(10,10);
B = rand(10,10);
Result = A-B;
%Identifty which elements are greater than 0
SlctElemnts = Result > 0 ;
%%Update all the elements in A that is less than 0 with the element in B
% A (Result<0);
disp(A)

Réponse acceptée

Rik
Rik le 26 Mai 2018
How about something like this:
A = rand(10,10);
B = rand(10,10);
Result = A-B;
LogicalIndex= Result<0;
A(LogicalIndex)=B(LogicalIndex);
  1 commentaire
Zaharaddeen Hussaini
Zaharaddeen Hussaini le 27 Mai 2018
So B will take in Logical Index as well apparently. Missed that out. Thank you

Connectez-vous pour commenter.

Plus de réponses (1)

Ameer Hamza
Ameer Hamza le 26 Mai 2018
Modifié(e) : Ameer Hamza le 26 Mai 2018
For indexing based solution, refer to Rik's answer. But in this specific case, you are trying to find the element-wise maximum value from both A and B. So you can do it faster using max() function,
A = max(A, B)
  1 commentaire
Zaharaddeen Hussaini
Zaharaddeen Hussaini le 27 Mai 2018
This works in my case as well like you pointed out. However index based solution is what I needed here. Thank you

Connectez-vous pour commenter.

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!

Translated by