importing only one element of a matrix into a function

1 次查看(过去 30 天)
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 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by