The replacement is slow. is there a better way?

1 view (last 30 days)
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 %%%

Accepted Answer

Mohammad Abouali
Mohammad Abouali on 18 Sep 2014
Edited: Mohammad Abouali on 18 Sep 2014
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!