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).

Patapunfate
  • 297
  • 1
  • 4
  • 12
  • 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,...)` – Dan May 05 '16 at 08: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 :-) – tim May 05 '16 at 08:14
  • 1
    Also have a look here: https://de.mathworks.com/help/matlab/matlab_prog/access-data-in-a-cell-array.html -> 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 :-) – tim May 05 '16 at 08:24
  • Also pretty nicely explained here: http://stackoverflow.com/a/9055336/701049 :-) – tim May 05 '16 at 08:25

1 Answers1

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.

Community
  • 1
  • 1
Dan
  • 45,079
  • 17
  • 88
  • 157
  • 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" : ". – Patapunfate May 05 '16 at 09:23
  • 1
    Oh god I did not see it, my apologies, now it works. Thank you – Patapunfate May 05 '16 at 11:27