Vectorizing Loops over Matrix Elements
1 view (last 30 days)
Show older comments
I was wondering if there is a general method for vectorizing code of the form:
A = zeros(n,m);
for i = 1:n
for j = 1:m
A(i,j) = f[i, j]
end
end
where f[i,j] is some function of i and j.
0 Comments
Accepted Answer
Torsten
on 23 May 2023
Moved: Torsten
on 23 May 2023
If the function f "knows" how to cope with two vectors as inputs that don't have the same size: Yes.
E.g.
n = 3;
m = 5;
f = @(i,j) i.'*j;
A = f(1:n,1:m)
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!