フィルターのクリア

Equivalent of A&(~B) without using ~

6 ビュー (過去 30 日間)
Yi-xiao Liu
Yi-xiao Liu 2022 年 6 月 22 日
コメント済み: Yi-xiao Liu 2022 年 6 月 22 日
I am looking for a way to generate the same output as A&(~B) w/o using the ~ operator. The problem is both A and B are very large sparse matrices with only a few non-zero elements (trues). Evaluating ~B will produce a temporary matrix that cannot fit in memory. What's the best way to circumvent this?
  1 件のコメント
David Goodmanson
David Goodmanson 2022 年 6 月 22 日
Hi YL,
does
a-(a&b)
get it done?

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

採用された回答

Yi-xiao Liu
Yi-xiao Liu 2022 年 6 月 22 日
編集済み: Yi-xiao Liu 2022 年 6 月 22 日
Hat tip to @David Goodmanson and @Stephen23. I found 4 solutions:
A=rand(5)<0.6;B=rand(5)>0.2;
ref=A&(~B);
Method1=A;
Method1(B)=false;
Method2=A-(A&B);
Method3=(A-B)>0;
Method4=xor(A,A&B);
all((Method1==ref)&(Method2==ref)&(Method3==ref)&(Method4==ref),"all")
ans = logical
1
rng("shuffle")
t1=nan(10,1);t2=nan(10,1);t3=nan(10,1);t4=nan(10,1);
for ii=1:10
idx=ceil(1e6*rand(2e6,2));
idx=unique(idx,"rows");
i=idx(:,1);j=idx(:,2);
A=sparse(i(1:1e6),j(1:1e6),true(1e6,1),1e6,1e6);
B=sparse(i((1e6+1):end),j((1e6+1):end),true(numel(i)-1e6,1),1e6,1e6);
tic
Method1=A;
Method1(B)=false;
t1(ii)=toc;
tic
Method2=A-(A&B);
t2(ii)=toc;
tic
Method3=(A-B)>0;
t3(ii)=toc;
tic
Method4=xor(A,A&B);
t4(ii)=toc;
end
t1=mean(t1);t2=mean(t2);t3=mean(t3);t4=mean(t4);
bar([t1,t2,t3,t4])
Personally I like the 4th one the most. It's fast, it's short, and it does not invoke type conversion in case you are short on Bytes.
  4 件のコメント
Paul
Paul 2022 年 6 月 22 日
編集済み: Paul 2022 年 6 月 22 日
I thought there was a concern about memory consumption, so thought it worthwile to point out that Method2 requires 8x more memory, in addtion to the need to cast to logical if used later on as logical.
Yi-xiao Liu
Yi-xiao Liu 2022 年 6 月 22 日
@Paul You are absolutely right. This is in fact (one of) the reason I prefer method 4 (see last line of my answer).
On conversion back to logical, my experience is that MATLAB will do the conversion for you if you try to use double as logical. there is no need to do it explicitly.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by