importing only one element of a matrix into a function

조회 수: 1 (최근 30일)
Sherwin
Sherwin 2022년 4월 26일
답변: Voss 2022년 4월 26일
Hi, I have a loop which calls a function in it. The function needs only one element of the matrix that the loop is counting. Can I use such a code?
O = [1 2; 3 4];
for i = 1:size(O,1)
f = M (O(i,1));
end
where M is a function? if yes, can i use O(i,1) in the function as the arguments??
if no, what do you suggest instead? If I import the entire O, I can't run the function M only on one element of O, I would have to have a loop in the function M and run M on all elements of O and this is not the output that I need.

채택된 답변

Voss
Voss 2022년 4월 26일
Yes, that's fine. (Try it and see.)
The only thing I would point out is that you probably don't want to overwrite f each time, but I assume that's an artifact of how the sample code is set up here and is not a problem in your real code.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by