MATLAB Answers



Asked by mona
on 23 Apr 2013

I am working on this piece of code which is basically k-means clustering and I'm really stuck at a point where cell needs to be reshaped into matrix:


with error: Error using reshape To RESHAPE the number of elements must not change.

Any help would be highly appreciated.



No products are associated with this question.

1 Answer

Answer by Matt J
on 23 Apr 2013

Do you understand why the following example produces an error? The error your code gives is due to the same thing.

>> reshape(rand(2,3),5,1)
Error using reshape
To RESHAPE the number of elements must not change.


Matt J
on 23 Apr 2013

Are you sure that the problem is the flexibility of RESHAPE? It's not possible that you've simply passed bad argument values to it? What kind of more flexible operation are you hoping to do?

on 23 Apr 2013

Alright, that would require some debugging and entering values accordingly. One question, do you think the code is okay in terms of converting double to cell since L is of type double. I have had an eye on it but put it down anyway, what do you think?

Matt J
on 23 Apr 2013

Did you understand the example I posted? The error you're getting pertains to the sizes and values of the arguments, not their data type.

By entering the values of L and length(codebook), RESHAPE expects that the array you're trying to reshape has L*length(codebook) elements in it. That is apparently not the number of elements in the array cell2mat(codebook). If this surprises you, use DBSTOP as I suggested to see how many elements there actually are.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!