check if random numbers( 5x5) generated are greater than (1x5)

조회 수: 1 (최근 30일)
JL
JL 2019년 8월 2일
편집: Walter Roberson 2019년 8월 2일
I've generated these from rand(5,5)
0.1556 0.2725 0.0095 0.3996 0.8905
0.0614 0.7758 0.8284 0.2179 0.1670
0.6611 0.3314 0.9085 0.8816 0.4252
0.0186 0.6031 0.7053 0.9421 0.3269
0.2911 0.1840 0.2433 0.9490 0.5676
and I would like to check if each one of them are greater than
x = [0.01 0.01 0.01 0.01 0.02]
and I should get something like this:
1 1 0 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
  댓글 수: 2
John Doe
John Doe 2019년 8월 2일
편집: John Doe 2019년 8월 2일
The algorithm can be as of:
Intialize a matrix with zeros to save your final result. Initialize another matrix to save your "x"
Try using a for loop to scan the rows and colums and use an if loop to compare the corresponding values and set the final result in the zero matrix.
Walter Roberson
Walter Roberson 2019년 8월 2일
It looks to me as if you want a 1 output if the matrix entries are greater than any one of the x values. If so you can compare to min(x) and not bother comparing to the other entries.

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

채택된 답변

Stephan
Stephan 2019년 8월 2일
편집: Stephan 2019년 8월 2일
Here is an example using gt (greather than) and bsxfun :
A = repmat(1:5,5,1)
B = 5:-1:1
res = bsxfun(@gt,A,B)
where A is your rand matrix and B is your vector to compare with.
  댓글 수: 1
JL
JL 2019년 8월 2일
편집: Walter Roberson 2019년 8월 2일
Thanks a lot Stephan! its working! :). I have another question - was wondering if you could help? https://uk.mathworks.com/matlabcentral/answers/474653-call-certain-rows-to-form-a-list

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by