The replacement is slow. is there a better way?

1 次查看(过去 30 天)
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