Compare elements of an array with all elements in a matrix
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
KSSV
2021 年 7 月 27 日
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 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!