How to compare elements of two equal size matrices by logical conditionals?

1 view (last 30 days)
Hello everyone! I have been struggling with this...
I would like to compare the elements of matrix A with the elements of matrix B, then get in matrix C the elements which have accomplished the logical condition. All matrices having the same size (12 x 1).
Eg,: if A (1,1) <= B (1,1) , then C (1,1) = A (1,1). If NOT then C (1,1) = B (1,1)...
How can I formulate it? ...
Many thanks indeed. Best Regards, MF

Accepted Answer

madhan ravi
madhan ravi on 16 Oct 2018
for i = 1:numel(A)
if A(i)<=B(i)
C(i) = A(i)
else
C(i) = B(i)
end
end
  4 Comments
MARCELA FRAGOZO
MARCELA FRAGOZO on 16 Oct 2018
Thanks Steven, I have done it with as you suggest with a prealloc matrix plus logical indexing (isEqual) and (~isEqual) to make the respective comparison... Best Regards, MF

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 16 Oct 2018
Edited: Stephen23 on 16 Oct 2018
@MARCELA FRAGOZO: simpler and more efficient:
C = min(A,B)
Try it, and you will see that it does the same as your logic.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!