0

I can't figure out the appropriate syntax to do this. I have 4 vectors that each have 15 elements. I want to extract a vector of length 4 containing the first element of each of my original vectors, and then do things with it. Then I want to do the same things with the second element of each vector, etc. and store all the answers in a matrix or array. Something like this:

for i = 1:15
    new_vec=zeros(4);
    n=1;
        for fc = {vec_A, vec_B, vec_C, vec_D}
            new_vec(n)=fc(i);
            n=n+1;
        end
     Final_answers{i}=functionDoThings(new_vec);
end

But I get:

> The following error occurred converting from cell to double: Error
> using double Conversion to double from cell is not possible. Error in
> my_script (line 31)
>             new_vec(n)=fc(i);

I feel like there is a simpler way to do this that I am missing.

1
  • 2
    Put the 4 vectors together into a 4x15 matrix (which is probably what they should have been in the first place) and then take each 4x1 column of the matrix. Commented Jan 26, 2017 at 22:09

2 Answers 2

1

Your inner loop is looping over a 1x4 cell:

for fc = {vec_A, vec_B, vec_C, vec_D}
    new_vec(n)=fc{1}(i)
    n=n+1;
end

This results in fc being a 1x1 cell in each iteration.

To access the actual data inside the cell you would need to use curly brackets:

for fc = {vec_A, vec_B, vec_C, vec_D}
    new_vec(n)=fc{1}(i)
    n=n+1;
end

{1} will access the first cell of fc and (i) the desired element.

However it is easier and faster to use a matrix like beaker suggested:

fc = [vec_A; vec_B; vec_C; vec_D];
for i = 1:15
    new_vec = fc(:,i);

    ...
end
Sign up to request clarification or add additional context in comments.

5 Comments

You should probably pull fc out of the loop. There's no need to generate the same array 15 times.
valid point! I was just unraveling the inner loop, without thinking about any further improvements.
Thanks this was just exactly what I needed. Your fc gave me a 64x1 matrix though, so I ended up with: fc = [vec_A, vec_B, vec_C, vec_D]; for i = 1:15 new_vec = fc(i,:); ... end
fc = [vec_A, vec_B, vec_C, vec_D]; for i = 1:15 new_vec = fc(i,:); ... end
ah yeah, it depends whether your vec_A, vec_B, vec_C, vec_D are 1x15 or 15x1 Arrays. In my example they would be 1x15.
0

The error tells that fc(i) is returning a cell while it tries to put it in the array as a double. Obviously, you can't convert an array to a single element.

So, instead of storing it into an array it should be stored into a cell.

for i = 1:15
    new_cell=cell(4);
    n=1;
    for fc = {vec_A, vec_B, vec_C, vec_D}
        new_cell{n}=fc(i);
        n=n+1;
    end
 Final_answers{i}=functionDoThings(new_cell);
end

Hope that helps!

1 Comment

That would help but functionDoThings needs an array of doubles. Thanks though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.