MATLAB: Logical Indexing instead of loops in 3D-Matrices

3d matrixif conditionlogical indexinglogical operationsloopMATLABvectorization

Dear All Community members,
I have two 3D matrices, AB1/AB2=1250x441x451 and two arrays, C=1×5 and D=1×1250.
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

Best Answer

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{:});