Converting cell array to matrix

조회 수: 4 (최근 30일)
Manisha Mehra
Manisha Mehra 2011년 3월 2일
I was doing my project on huffman coding on a gray scale image. I have three arrays
  1. code
  2. level_of_gray
  3. length_of_code
>> whos code
Name Size Bytes Class Attributes
code 256x1 33374 cell
>> whos level_of_gray
Name Size Bytes Class Attributes
level_of_gray 256x1 2048 double
>> whos length_of_code
Name Size Bytes Class Attributes
length_of_code 256x1 2048 double
each cell of(code) has characters(strings of 0's and 1's upto length 16)
I want to make a code_dictionary=[level_of_gray;length_of_code;code]
but MATLAB is not allowing to do it. I have tried cell2mat (code). But this is also not working.
  댓글 수: 4
Walter Roberson
Walter Roberson 2011년 3월 2일
You cannot leave the codes in character representation if you want to form a numeric array. Only cell arrays can contain a mix of numbers and characters.
Manisha Mehra
Manisha Mehra 2011년 3월 2일
exactly,I don't need a numeric array.
Thanks

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

채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 2일
code_dictionary = [num2cell(level_of_gray), num2cell(length_of_code), code]
This will produce a 256 x 3 cell array.
Trying to convert your code cell to matrix form is not going to work because your codes are variable length. In order to put them in to matrix form, you would have to make them all the same length, such as by padding the shorter ones with values other than 0 and 1 that the rest of your code would have to know to detect and throw away.
So what you could do is:
code_dictionary = [level_of_gray, length_of_code,
cell2mat(cellfun(@(V) [V - '0', nan(1,16-length(V))], code, 'Uniform', 0)))];
This would produce a 256 x (2+16) matrix of doubles. The first column would be the gray level, the second column would be the code length, and the 3rd to 18th columns would be the values of the code, with '0' and '1' transformed to 0 and 1, and with NaN values in all unoccupied places.
  댓글 수: 3
Walter Roberson
Walter Roberson 2011년 3월 2일
There will be no way to call them by their array names.
level_of_gray = vertcat(code_dictionary{:,1});
code = code_dictionary(:,3);
Manisha Mehra
Manisha Mehra 2011년 3월 3일
thanks you so much

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by