I have a code segment as follows:
f = @(x) [x(1)^2 ; x(2)^2]
X = [2; 3]
When I type f(X), I simply obtain the following:
f(X)
ans =
4
9
Now, think that there is a for loop going from 1 to 2. According to value of loop, I want to extract the row which is the value of loop. For instance:
for i=1:2
when i=1 it will extract 1st element, which is: 4
when i=2 it will extract 2nd element, which is: 9
end
I did this by the following:
for i=1:2
X1 = f(X)
Result = X1(i)
end
This works perfectly well. However, I do not want to define such intermediate variables. Is it possible directly to extract the certain row of the function?
Another way of doing is is to create a unit vector and multiply the function itself, but I do not want this as well. What I mean is the following:
when i=1; unit = [1 0]
[1 0]*[2;4] = 2
when i=2; unit = [0 1]
[0 1]*[2;4] = 4
This also works well but I do not want this.
I just want to call directly f(X) but give me the row desired.
Thanks in advance!