필터 지우기
필터 지우기

Efficient matrix comparisons that retain row/column order

조회 수: 2 (최근 30일)
Rachel
Rachel 2012년 5월 26일
Hi Everyone,
I've been struggling to figure out an efficient way to compare matrices and have the results stored in a matrix of a comparable size and similar ordering rather than in a vector of scalars for the case where the comparison is true. Can anyone share with me a faster way of doing this than just using for loops?
For example, suppose we start with:
mat1 = rand(10,10);
mat2 = rand(10,10);
difr = mat1 - mat2;
What I want to do is create a new matrix called mat3 (that is of the same dimensions as mat1 and mat2) where each element mat3(i,j) = mat1(i,j) if difr < 0.2 but 0 otherwise.
To do this, I make my best guess and try using the following code (whch I know is incorrect):
mat3 = zeros(10,10);
mat3 = mat1(diff < 0.2)
This turns mat3 into an Nx1 matrix where N is the number of times that the comparison is true ... and all of the information about position (e.g. i,j) in the original mat1 matrix is lost.
Can anyone share the proper way of doing this?
Thank you,
R

채택된 답변

per isakson
per isakson 2012년 5월 26일
This would be my first trial:
mat3 = mat1;
mat3( diff >= 0.2 ) = 0;
This isn't magic it is logical indexing!

추가 답변 (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