Conditionally replace matrix elements from other corresponding matrix
Show older comments
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
1 Comment
Matthew Knights
on 11 Aug 2015
Accepted Answer
More Answers (1)
Star Strider
on 11 Aug 2015
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);
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!