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.
  1 件のコメント
Pierre Bushel
Pierre Bushel 2014 年 9 月 18 日
Thanks so much Mohammad!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by