Vectorization of an arrayfun problem

3 views (last 30 days)
Consider a AxB matrix M with A>1 & B>1. I need to perform the following operation:
Output=arrayfun(@(input) sum(p_matrix.*max(input,M),'all'), M);
For each 1x1 entry of M (input), max(input,M) returns a AxB matrix with the greatest numbers between "input" and each entry of M, element-wise. The result is multiplied by AxB p_matrix, element-wise, and its elements are summed up. The result is saved in the corresponding entry of Output, a AxB matrix as well.
Arrayfun slowly loops over each element of M, one by one. Is there any way to build a vectorized version of this command?
Thanks sincerely.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Apr 2021
Edited: Walter Roberson on 4 Apr 2021
rng(655321)
A = 10; B = 15;
M = randi(10, A, B)
M = 10×15
4 2 2 6 1 8 7 10 9 2 6 9 7 9 4 4 8 1 5 2 3 1 5 3 2 2 6 7 4 10 2 3 10 1 2 9 8 9 8 8 5 4 3 3 3 9 5 8 8 8 7 8 10 5 9 4 2 5 8 1 8 1 8 1 2 2 3 6 7 1 4 4 2 4 5 4 6 6 1 3 10 5 1 6 5 8 2 3 8 8 8 10 8 9 1 5 10 10 7 3 10 5 2 2 5 10 2 1 8 3 1 4 1 3 5 5 5 5 3 1 7 5 6 1 3 5 6 5 2 2 7 2 4 6 1 2 4 5 6 3 1 10 6 5 8 7 10 7 5 2
p_matrix = randi([-5 5], A, B)
p_matrix = 10×15
-2 1 -1 -1 -1 1 4 -3 4 -4 0 5 3 4 -3 1 2 -1 -4 -1 -1 -3 -3 0 0 2 1 4 1 2 -5 4 1 -1 3 3 3 5 -3 4 -3 3 -5 1 0 2 3 0 -4 -5 1 0 4 5 -2 -3 0 -3 -2 5 5 5 1 -1 -4 -1 -5 -4 1 3 2 2 -1 -1 5 -1 -2 -2 1 3 2 5 2 -3 -5 -2 4 5 1 0 -3 5 -4 1 1 -5 3 0 3 -5 5 5 5 -3 -5 1 5 0 5 -1 1 -2 -4 -3 -2 -1 5 1 0 -1 -4 3 2 -2 5 -3 1 3 5 5 -2 2 -1 2 -3 4 1 -2 -5 5 3 0 -2 -3 2 2 -5 -2 -1 4
tic
Output = arrayfun(@(input) sum(p_matrix.*max(input,M),'all'), M);
toc
Elapsed time is 0.007977 seconds.
Output
Output = 10×15
419 368 368 462 364 484 468 540 501 368 462 501 468 501 419 419 484 364 443 368 392 364 443 392 368 368 462 468 419 540 368 392 540 364 368 501 484 501 484 484 443 419 392 392 392 501 443 484 484 484 468 484 540 443 501 419 368 443 484 364 484 364 484 364 368 368 392 462 468 364 419 419 368 419 443 419 462 462 364 392 540 443 364 462 443 484 368 392 484 484 484 540 484 501 364 443 540 540 468 392 540 443 368 368 443 540 368 364 484 392 364 419 364 392 443 443 443 443 392 364 468 443 462 364 392 443 462 443 368 368 468 368 419 462 364 368 419 443 462 392 364 540 462 443 484 468 540 468 443 368
tic
Output2 = reshape(sum(p_matrix .* max(M, reshape(M, 1, 1, [])), [1 2]), size(M));
toc
Elapsed time is 0.005653 seconds.
Output2
Output2 = 10×15
419 368 368 462 364 484 468 540 501 368 462 501 468 501 419 419 484 364 443 368 392 364 443 392 368 368 462 468 419 540 368 392 540 364 368 501 484 501 484 484 443 419 392 392 392 501 443 484 484 484 468 484 540 443 501 419 368 443 484 364 484 364 484 364 368 368 392 462 468 364 419 419 368 419 443 419 462 462 364 392 540 443 364 462 443 484 368 392 484 484 484 540 484 501 364 443 540 540 468 392 540 443 368 368 443 540 368 364 484 392 364 419 364 392 443 443 443 443 392 364 468 443 462 364 392 443 462 443 368 368 468 368 419 462 364 368 419 443 462 392 364 540 462 443 484 468 540 468 443 368

More Answers (0)

Categories

Find more on Elementary Math in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!