Compare elements of an array with all elements in a matrix
1 view (last 30 days)
Show older comments
I have a matrix P (18x10), and one array C (1x10). I want to compare each element of C with each element of each row in P (see if elements in C are bigger than elements in P).
(As an illustration, I want to see if: C(1)> P(1,1); C(1)> P(1,2); ... C(2)>P(2,1); C(2)>P(2,2)...).
P = randn(18,10);
C = randn(1,10);
I tried to do this as follows:
for a = 1:length(C)
for m = 1:size(P,2)
for m_row = 1:size(P,1)
logvec= length(C(a))>P(m_row, m:end);
end
end
end
I get the following error: Unable to perform assignment because the left and right sides have a different number of elements.
I expect as an output a matrix 18x10, with 0s and 1s.
I'd appreciate your help! Thank you!
0 Comments
Accepted Answer
KSSV
on 27 Jul 2021
No loop needed. You can use straight away like shown:
P = randn(18,10);
C = randn(1,10);
C1 = repmat(C,18,1) ;
idx = C1 > P
3 Comments
More Answers (1)
See Also
Categories
Find more on Matrix Indexing 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!