# Compare elements of an array with all elements in a matrix

조회 수: 1(최근 30일)
Arlinda Gashi 2021년 7월 27일
댓글: KSSV 2021년 7월 27일
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!

댓글을 달려면 로그인하십시오.

### 채택된 답변

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표시숨기기 이전 댓글 수: 2
KSSV 2021년 7월 27일
@Chunru yes you are right. It is redundant. Can be removed.

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

Chunru 2021년 7월 27일
P = randn(5,4)
P = 5×4
0.4430 1.0634 -0.8297 -0.4101 -1.1517 -2.1151 1.2150 0.3174 1.2722 1.9258 -2.0465 -0.1428 0.2521 1.2782 -0.3429 -0.4121 0.4291 0.8109 -1.9595 -0.0784
C = randn(1,4)
C = 1×4
1.3790 -1.2752 -0.2469 -0.1746
lv = C > P
lv = 5×4 logical array
1 0 1 1 1 1 0 0 1 0 1 0 1 0 1 1 1 0 1 0
##### 댓글 수: 1표시숨기기 없음
Arlinda Gashi 2021년 7월 27일
Amazing! Thanks a lot!

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by