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
B(:,:,idx, idz) = (A(:,:,idx, idz)>thres(idx, idz)).*A(:,:,idx,idz);
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?
newThresh = repelem(thres(:), M, N);
newThresh = reshape(newThresh',M,N,O,P);