2

I have a big cell array A=cell(a,b,c,d) and a row vector B with dimensions 1-by-b.

I want to build a loop in MATLAB that does the following:

for i=1:n
   B = Calculate_row(input1,input2) %this is a function that creates my B row
   A{a,:,c,i} =  B(:)
end

anyway if I try to do A{a,:,c} = B(:) I receive the following error:

Expected one output from a curly brace or dot indexing expression, but there were b results.

And if I try to do A(a,:,c) = B(:) I receive the following error:

Conversion to cell from double is not possible.

Is there a way to do this? (I know a less elegant way that probably works would be to assign each value to the cell separately, but I would prefer not to do it).

4
  • 1
    You probably just want this A(a,:,c) = {B(:)}. However, the way you call Calculate_row (if that is indeed how you are calling it) is really bad practice. Never call a script from another script, make sure you only call functions. Adapt Calculate_row to be a function that might look something like B = Calculate_row(input1, input2,...) Commented May 5, 2016 at 8:13
  • 1
    Did you miss out something? You assign it in a loop but you are not using i as an index somewhere in the assignment - furthermore A has 4 dimensions, but you are only giving 3 dimensions in the assignment. Did you want to do this A{a,:,c,i} = B(:)? (just guessing). Please give a value for the 4th dimension and try the assignment again :-) Commented May 5, 2016 at 8:14
  • 1
    Also have a look here: de.mathworks.com/help/matlab/matlab_prog/… -> Depending on your data you might not even need Cells, I don't know. But if not required, then rather don't use it as it makes things much more complex :-) Commented May 5, 2016 at 8:24
  • Also pretty nicely explained here: stackoverflow.com/a/9055336/701049 :-) Commented May 5, 2016 at 8:25

1 Answer 1

1

One way to do this is to make B a cell array and then take advantage of comma-separated-lists:

B_cell = num2cell(B);
[A{a,:,c}] = B_cell{:}    %// or [A{a,:,c,i}] = B_cell{:} if tim's comment is correct

Have a look at Loren Shure's article Deal or No Deal and also this answer for more.

The problem with your syntax, A{a,:,c} = B(:), is that the RHS (i.e. B(:)) is just one single matrix whereas the LHS is a comma-separated-list of b results. So you are basically requesting that 1 output be assigned to b variables and MATLAB doesn't like that, also hence the error message.

The problem with A(a,:,c) = B(:) is that indexing a cell array with () returns a cell array and you can't just assign a matrix (i.e. B(:)) to a cell array hence you second error.

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

2 Comments

Thank you Dan, I did as you suggested me but this way I still receive an error. When I type [A({1,:,1,1}] = B_Cell{:} I receive the error [A({1,:,1,1}] = B_Cell{:} ↑ Error: Unexpected MATLAB operator. With the arrow pointing at the" : ".
Oh god I did not see it, my apologies, now it works. Thank you

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.