importing only one element of a matrix into a function
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Voss
on 26 Apr 2022
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 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!