Updating an array elements with elements from another Array

조회 수: 10 (최근 30일)
Zaharaddeen Hussaini
Zaharaddeen Hussaini 2018년 5월 26일
댓글: Zaharaddeen Hussaini 2018년 5월 27일
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)

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2018년 5월 26일
편집: Ameer Hamza 2018년 5월 26일
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
Zaharaddeen Hussaini
Zaharaddeen Hussaini 2018년 5월 27일
This works in my case as well like you pointed out. However index based solution is what I needed here. Thank you

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by