필터 지우기
필터 지우기

Conditionally replace matrix elements from other corresponding matrix

조회 수: 2 (최근 30일)
I have two matrices of the same size, X and Y.
I want to replace elements of X if certain conditions are met, with the corresponding element of matrix Y, but crucially without using a loop.
The conditions are: X<Y/exp(1)&&X>1e3
i.e. I was trying something like: X(X<Y/exp(1)&&X>1e3)=Y, but obviously this doesn't work.
What would be the best way to acheive this?
Thanks,
Matt

채택된 답변

Titus Edelhofer
Titus Edelhofer 2015년 8월 11일
Hi Matt,
two changes: you need to use "&" instead of "&&" to work for vectors, and you need to index into Y the same way.
idx = X<Y/exp(1) & X>1e3;
X(idx) = Y(idx);
Titus

추가 답변 (1개)

Star Strider
Star Strider 2015년 8월 11일
This runs. I will leave it to you to determine if it works with your matrices:
X = randi(1E+5, 10);
Y = randi(1E+5, 10);
logidx = X<Y/exp(1) & X>1e3; % Logical Index Array
X(logidx) = Y(logidx);

카테고리

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