Logical Indexing instead of loops in 3D-Matrices

5 次查看(过去 30 天)
Dear All Community members,
I have two 3D matrices, AB1/AB2=1250x441x451 and two arrays, C=1x5 and D=1x1250.
Instead of running loops I want to use a vectorization code to do the following operation;
If D(i)<=C (for each value in C)
ABC(i)=AB1(i)
else
ABC(i)=AB2(i).
As an end-result I want five different 3D matrices ("ABC1", "ABC2"..."ABC5"), one for each value in C.
In order to use vectorization I could repeat the 3D matrices ("AB1" and "AB2") and the 1D array ("D") 5 times using repmat and repeat the 1D array ("C") 1250 times using repelem. But I am not sure how to proceed any further or even if this is the best approach..
How do I apply the logical condition (D<C) to the 3D matrices?
All help is greatly appreciated.
Kind regards
  4 个评论
Bruno Luong
Bruno Luong 2020-8-28
编辑:Bruno Luong 2020-8-28
Your example is incoherent with the question, you said in the question that length(D) matches size(AB1,1) and size(AB2,2).
But then in your example C is match, not D.
You also seem to reverse the comparison.
If you run this it gives the results you expect, but it's not exactly as with your question. Let you sort it out:
AB1=[1 2 3;4 5 6; 7 8 9; 10 11 12];
AB2=[13 14 15; 16 17 18; 19 20 21; 22 23 24];
D=[1:1:4];
C=[0.5:1:2.5];
k=double((D(:)<=C(:).'));
AB12=[AB1(:),AB2(:)];
n = size(AB12,1);
k = reshape(k,size(AB1,1),1,1,[]);
i = reshape(1:n,size(AB1));
ABC12=AB12(i+n*k);
ABC12_cell=num2cell(ABC12,[1 2 3]);
[ABC1,ABC2,ABC3]=deal(ABC12_cell{:})
J. Alex Lee
J. Alex Lee 2020-8-28
what does it mean that the third dimension is (0.1:0.1:0.3)? For your purposes does it matter at all?

请先登录,再进行评论。

采纳的回答

Bruno Luong
Bruno Luong 2020-8-28
编辑:Bruno Luong 2020-8-28
This is code according to the specification stated in the question, NOT the example that is incoherent.
% Dummy test data
AB1=rand(250,441,451);
AB2=rand(250,441,451);
D=rand(1,size(AB1,1));
C=rand(1,5);
k=double((D(:)>C(:).'));
AB12=[AB1(:),AB2(:)];
n = size(AB12,1);
k = reshape(k,size(AB1,1),1,1,[]);
i = reshape(1:n,size(AB1));
ABC12=AB12(i+n*k);
ABC12_cell=num2cell(ABC12,[1 2 3]);
[ABC1,ABC2,ABC3,ABC4,ABC5]=deal(ABC12_cell{:});
  5 个评论
J. Alex Lee
J. Alex Lee 2020-8-28
+1 for neat vectorized code and >2D thinking (bow down)

请先登录,再进行评论。

更多回答(1 个)

J. Alex Lee
J. Alex Lee 2020-8-28
What is the purpose of vectorization? Is it for imagined speed gains, or just an "in principle" question?
Bruno's answer is fascinating in principle, but is undecipherable for the likes of me, but also signifcantly slower than loops (if I'm doing the right thing)
% Dummy test data
AB1=rand(250,441,451);
AB2=rand(250,441,451);
C=rand(1,5);
D=rand(1,size(AB1,1));
%% Bruno's answer
tic
k=double((D(:)>C(:).'));
AB12=[AB1(:),AB2(:)];
n = size(AB12,1);
k = reshape(k,size(AB1,1),1,1,[]);
i = reshape(1:n,size(AB1));
ABC12=AB12(i+n*k);
ABC12_cell=num2cell(ABC12,[1 2 3]);
[ABC1,ABC2,ABC3,ABC4,ABC5]=deal(ABC12_cell{:});
%% loops to achieve same thing
tic
ABCD = cell(size(C));
for k = 1:length(C)
ABCD{k} = AB1;
mask = D > C(k);
ABCD{k}(mask,:,:) = AB2(mask,:,:);
end
toc
%% check answers between methods
% as Rik points out, this is why naming variables serially is silly.
isequal(ABCD{1},ABC1)
isequal(ABCD{2},ABC2)
isequal(ABCD{3},ABC3)
isequal(ABCD{4},ABC4)
isequal(ABCD{5},ABC5)
The result is
Elapsed time is 11.659369 seconds.
Elapsed time is 3.062867 seconds.
  2 个评论
Askeladden2
Askeladden2 2020-8-28
For this specific example it was just speed gain imagination!
I also voted +1 for this simple and neat code.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by