Fill a nxm matrix with vectorised expression

3 次查看(过去 30 天)
Hi all,
here my doubt: i have a matrix M with dimensions nxm. The matrix is initially initialised with zeros, and i fill each M(i,j) calling another function:
M = zeros(n,m)
for i = 1:n
for j = 1:m
M(i,j) = another_function(several_inputs);
end
end
is it possible to fill the matrix M without the for loops, but instead using a vectorised expression?
Thanks
  4 个评论
Andrea Agosti
Andrea Agosti 2020-10-16
Hi, thanks for your aswer. The inputs of the another_function are different for each i,j.
Thanks
Konstantin
Konstantin 2020-10-16
sometimes it works to vectorize the function itself
matrix = another_function(matrix)

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2020-10-16
编辑:Matt J 2020-10-16
No, there is no way to vectorize an element-wise function generically, without considering the specifics of the function. You can avoid for-loop syntax by using arrayfun, but this simply hides the loops from view, it doesn't replace them with something more efficient.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by