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

Konstantin
Konstantin 2020년 10월 16일
What is the condition for selecting the function? Is the function different for each cell of the matrix, or is it possible, that the same function will be used for several cells?
KSSV
KSSV 2020년 10월 16일
It depends on what your another_function is.
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일

0 개 추천

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개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2020년 10월 16일

댓글:

2020년 10월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by