Obtain coordinate of a vector or multivalued function

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
ans = 0.1073
ans = -0.1436
ans = 0.4184
ans = 1.7885
ans = 0.7335
ans = 1.2884
ans = 0.2352
ans = 0.7480
ans = 1.8433
ans = 1.1062

2 Comments

I see. It is not possible to just say f(w,x,y,z)(3), one has to store it in a new variable like k=f(w,x,y,z) and then say k(3). Thanks!
You can use:
f3 = f(a(3),b(3),c(3),d(3))

Sign in to comment.

Categories

Asked:

on 28 Apr 2022

Commented:

on 28 Apr 2022

Community Treasure Hunt

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

Start Hunting!