Fill a nxm matrix with vectorised expression
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Andrea Agosti
am 16 Okt. 2020
Kommentiert: Konstantin
am 16 Okt. 2020
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 Kommentare
Konstantin
am 16 Okt. 2020
sometimes it works to vectorize the function itself
matrix = another_function(matrix)
Akzeptierte Antwort
Matt J
am 16 Okt. 2020
Bearbeitet: Matt J
am 16 Okt. 2020
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!