# How to efficiently compare large multi-dimensional arrays together of different dimensions?

3 views (last 30 days)
Augustus Elton on 23 Sep 2021
Commented: Augustus Elton on 23 Sep 2021
Hi,
I am working with large dimensional arrays and I wish to compare a 4-D array to a 2-D array. Each 2-D element in the 4-D array is to be compared with one component of the 2-D array, as follows
%% Initial Problem
clc; clear;
% Array Dimensions
M = 512;
N = 512;
O = 136;
P = 6;
% Generate random matrix
A = rand(M, N, O, P);
thres = rand(O, P); % Threshold values
if ~exist('B','var')
B = zeros(size(A));
end
% 1st Method: (for loops) Compute thresholded matrix B
tic
for idx = 1:O
for idz = 1:P
B(:,:,idx, idz) = (A(:,:,idx, idz)>thres(idx, idz)).*A(:,:,idx,idz);
end
end
t1 = toc
t1 = 2.3543
% ~ 1.5 seconds to complete, desktop (matlab2020a)
The most obviuos way to do this is to loop through the 3rd and 4th dimensions in a simple for loop configuration and compare each of the elements to each other. However this is quite slow and I wish to try and speed the process up as much as possible. Instead of doing this I replicated the threshold matrix, 'thres', to be the same size as the 'A' matrix so that a straight fowards comparison could be done. This is slightly faster, however more memory is required aswell as the reshape and repelem functions. (On the desktop version of MATLAB, this procedure cut down ~1.5 seconds to ~0.9 seconds, so it is roughly 0.6 seconds faster than using for loops). Is there any other ways to speed up this method up? Could a simpler method of indexing be used?
% 2nd Method: Make the thresh matrix have the same array dims as the A matrix
% Compute thresholded matrix B
tic
% Replicate array
newThresh = repelem(thres(:), M, N);
% Reshape array into 4-D
newThresh = reshape(newThresh',M,N,O,P);
% A bit faster
B = (A>newThresh).*A;
t2 = toc
t2 = 2.0163
% ~ 0.9 seconds to complete, desktop (matlab2020a)

Chunru on 23 Sep 2021
M = 512;
N = 512;
O = 136;
P = 6;
% Generate random matrix
A = rand(M, N, O, P);
thres = rand(O, P); % Threshold values
if ~exist('B','var')
B = zeros(size(A));
end
tic
% Replicate array
newThresh = repelem(thres(:), M, N);
% Reshape array into 4-D
newThresh = reshape(newThresh',M,N,O,P);
% A bit faster
B = (A>newThresh).*A;
t2 = toc
t2 = 2.0938
tic
newThresh = reshape(thres, 1, 1,O,P);
B = (A>newThresh).*A;
t3 = toc
t3 = 0.2953
Augustus Elton on 23 Sep 2021
Thankyou, much faster.

R2020a

### Community Treasure Hunt

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

Start Hunting!