Obtain coordinate of a vector or multivalued function
Show older comments
1.Given a vector v (with some number of coordinates) obtain its i-th coordinate.
What is the command for this? I'm expecting something like v[i]
2.Same question but with a function with multiple outputs, for example:
f=@(a,b,c,d) [a^2-b+c+d,a-b^2+c+d^3,a*b*c*d,a-b-c-d]; (in general a function created like this from m inputs to n outputs)
to obtain say the third coordinate of f(w,x,y,z) I would expect something like f(w,x,y,z)[3], what is the command?
Both could be the same question if the second is also a vector but I don't know how to check that.
Answers (1)
It should be v(i) not v[i]. Read about MATLAB indexing.
f=@(a,b,c,d) [a.^2-b+c+d,a-b.^2+c+d.^3,a.*b.*c.*d,a-b-c-d]; % anonymous function. Read about this
a = rand(10,1) ;
b = rand(10,1) ;
c = rand(10,1) ;
d = rand(10,1) ;
f = f(a,b,c,d) ;
for i = 1:10
f(i)
end
Categories
Find more on Resizing and Reshaping Matrices 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!