The replacement is slow. is there a better way?

조회 수: 1 (최근 30일)
Pierre Bushel
Pierre Bushel 2014년 9월 18일
편집: Mohammad Abouali 2014년 9월 18일
I search for indices in one matrix with double zeros and then replace them in another two matricies with NA. However, because there are 400,000 rows and 400,000 columns, the replacement is slow. Is there a better/faster way to do this. Here is the sample code:
[rows,cols,~] = find(binZ); % indices to elements with double-zeros
rrr(rows,cols) = NaN; % replace double-zeros with NaN's %%%%%%This step is slow %%%
RRR(rows,cols) = NaN; % replace double-zeros with NaN's %%%%%%This step is slow %%%

채택된 답변

Mohammad Abouali
Mohammad Abouali 2014년 9월 18일
편집: Mohammad Abouali 2014년 9월 18일
It seems rrr(binZ)=NaN; should also work for you. I am assuming binZ is a logical big matrix where the entry is true if that element is double zero (or whatever you mean by double zero).
NOTE: By the way, I DON'T think rrr(rows,cols)=NaN is a correct way of doing it. Let me give you an example:
>> A=ones(3,3)*2
A =
2 2 2
2 2 2
2 2 2
So I want to set, let's say, element (1,1), (1,3), and (3,2) to NaN;
>> rows=[1,1,3];
>> cols=[1,3,2];
>> A(rows,cols)=NaN
A =
NaN NaN NaN
2 2 2
NaN NaN NaN
While clearly this is wrong. The correct answer is:
>> A(sub2ind(size(A),rows,cols))=NaN
A =
NaN 2 NaN
2 2 2
2 NaN 2
It was assumed you have 2D arrays or (matrices) so they are all MxN arrays.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by