logical indexing on a matrix

Is there a brief way without using much loops to take "all of certain columns" of a matrix and evaluate whether they're larger than the first column of that marrix?
nSize = 10;
mymatrix = rand(nSize,5);
IndexToCompare = logical(round(mymatrix));
myBenchmark = mymatrix(:,1);
% A. I want to replace the following loop with some statement such as B which uses logical indexing but for a matrix rather than a vector
for i=1:nSize
result(i) = sum(mymatrix(IndexToCompare(i,:)) > myBenchmark(i,1));
end
%B. this is going to throw error due to size mismatch.
result = sum(mymatrix(IndexToCompare) > myBenchmark);

댓글 수: 1

madhan ravi
madhan ravi 2020년 9월 8일
Do you even realise the loop and the last line have two different in depth details? And that’s one of the reasons to give you a straight answer?

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

답변 (1개)

Walter Roberson
Walter Roberson 2020년 9월 8일

0 개 추천

all(mymatrix(IndexToCompare, :) > myBenchmark,1)
The result would be a vector of the same length as IndexToCompare, and which will be true for the columns corresponding to IndexToCompare in which each row entry is strictly greater than the corresponding entry in myBenchmark .
Note that the random generation you are doing can include the first column, and the first column can never be strictly greater than itself.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 9월 8일

답변:

2020년 9월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by