필터 지우기
필터 지우기

Equivalent of A&(~B) without using ~

조회 수: 3 (최근 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?

채택된 답변

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by