0

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!

nicomedian
  • 53
  • 1
  • 7
  • It's not clear what you are trying to do. what's the point of your loop? All it does at the moment is display each element to the screen and set `Result` to equal the last element...?. – Dan Mar 12 '14 at 07:56
  • Duplicate of http://stackoverflow.com/q/3627107/3382783 – Iban Cereijo Mar 12 '14 at 08:21

2 Answers2

0

You are defining your function as :

f = @(x) [x(1)^2 ; x(2)^2]

If you give x as a column vector, it would read row-wise. Thus, your row-indexing method of i=1,2 won't work, because the function would need first and second row with x(1) and x(2) at the same time.

I think, the best you can do is this -

X1 = f(X)
for i=1:2
    X1(i)
end

The best practice to solve your problem and still access each output from using the function on each element of X would be something like this -

f = @(x) x.^2;
X = [2 3];
X1 = f(X);

for i=1:numel(X)
    Result = X1(i);
    %// Do something with Result
end

Edit 1

f = @(x) x.^2;
X = [2 3];

for i=1:numel(X)
    Result = f(X(i)) %%// Instead of  f(X,1) you need f(X(1)) and so on..
    %// Do something with Result
end
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • How is this any different from the current loop (aside from moving the `f` calc out of the loop)? I don't see how row or column vectors make a difference here? – Dan Mar 12 '14 at 07:58
  • From what I understood of OP's question that he doesn't want intermediate values within that loop that runs row-wise. OP using two variables in same function definition is not the best method for sure. – Divakar Mar 12 '14 at 08:04
  • I agree with removing the 'intermediate' element, but I don't agree that it has anything to do with looping row-wise. Nor does that loop have any point that I can see. – Dan Mar 12 '14 at 08:07
  • Just edited for the "best" way to achieve OP's target. – Divakar Mar 12 '14 at 08:09
  • what I try to find is that is there a way to extract directly, such as, f(X,1) and it calculates f(X) but gives 1st row (or column based on the vector). I seek this solution. – nicomedian Mar 12 '14 at 10:37
  • See Edit 1. Though a vectorized approach would be better. – Divakar Mar 12 '14 at 10:49
0

Here's another possible way of doing it:

f = @(x,n) [x(n).^2];

Where n is the indices of x you want returned.

x = [3 5];
f(x,1:2)

ans =

     9    25

or

f(x,2)

ans =

    25
nkjt
  • 7,825
  • 9
  • 22
  • 28