how to make this faster?
3 views (last 30 days)
Walter Roberson on 13 Aug 2022
The programming model of
is as if it first compares each element of B to 1, creating a temporary logical array, and then calls the subsassgin function to assign 2 to the locations in A referred to by the logical array.
That is, the programming model calls for B==1 to be completely completed before the assignment starts. It does not internally optimize the code to a loop along the lines of
for K = 1 : numel(A)
if B(K) == 1; A(K) = 2; end
except in some internal programming language (generated machine code for example)
Would it be possible in theory to detect that the indexing expression is "simple" and optimize to this kind of loop? Perhaps. But it would have to be fairly constrained. For example,
A = zeros(3,5,'sym');
A(A <= 0) = -1
A(end) = B
A(A <= 0) = -2
The error message is wacky (should be an error about being unable to decide whether the comparison is true.
The important point here is to notice that A did not get changed by the failed assignment: that in the general case you have to fully evaluate the indexing expression first in case it fails.
In the simple case of a comparison of a double array to a constant, there cannot be a failure (provided the double array does not exceed the size of the original matrix... but there can be a failure if the double array is larger...)
So optimization would sometimes be possible, but mostly not.
What you could do in order to reduce the memory load is to process in chunks, such as a group of 10 three dimensional panes at a time. However if you do that then in order to get the assignment right you would have to calculate the locations of all of the indices using find() and sub2ind() -- because there is no way to ask for something like
A(logical2DArray, , 5)
meaning that you wanted the logical2DArray to expand to indicate the first two dimensions.
It would certainly be possible to reduce memory pressure using these techniques, but it would probably not be faster if you do have available memory.
Jan on 14 Aug 2022
A = rand(2000, 2000, 5000);
B = randi([0,3], 2000, 2000, 5000);
for k = 1:numel(A)
A(k) = 2;
parfor k = 1:numel(A)
A(k) = 2;
A = reshape(A, 4e6, 5000);
B = reshape(B, 4e6, 5000);
parfor k = 1:500
a = A(:, k);
a(B(:, k)==1) = 2;
A(:, k) = a;