RESHAPE command in MATLAB

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:
vec_dist(i)=dist(s(i),reshape(cell2mat(codebook),L,length(codebook)));
with error: Error using reshape To RESHAPE the number of elements must not change.
Any help would be highly appreciated.

답변 (1개)

Matt J
Matt J 2013년 4월 23일

1 개 추천

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.

댓글 수: 5

Matt J
Matt J 2013년 4월 23일
Use DBSTOP to investigate the values of size(cell2mat(codebook)), L and length(codebook) at the line that produces the error.
mona
mona 2013년 4월 23일
Thank you for the quick information. How would I change this command in order to make it more flexible in terms of dealing matrix of variable size?
Matt J
Matt J 2013년 4월 23일
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?
mona
mona 2013년 4월 23일
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
Matt J 2013년 4월 23일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2013년 4월 23일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by