Typecasting higher dimension matrices

조회 수: 14 (최근 30일)
Sean
Sean 2011년 9월 21일
답변: Carl Witthoft 2020년 3월 30일
I recently found (to my chagrin) that the typecast function only works on scalars and 1D matrices (i.e. arrays).
Does anyone know of a way to typecast matrices with 2, 3, 4, or more dimensions (other than simply indexing out and typecasting all the elements or 1st dimension arrays into a new variable?
  댓글 수: 2
Florin Neacsu
Florin Neacsu 2011년 9월 21일
Hello,
What do you mean?
A=[2,2;1,3];
B=single(A);
seems to work.
Regards,
Florin
Sean
Sean 2011년 9월 21일
The 'single' command casts the data to the new class, but it truncates any data that is considered 'out of range' for the new class to the bounds of the new class. (e.g. uint8(int8(-5))==0)
On the other hand, 'typecast' keeps the data, so:
typecast(int8(-5),'uint8')==251
The problem is the typecast function only accepts scalars and 1D arrays.

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

채택된 답변

Walter Roberson
Walter Roberson 2011년 9월 21일
Suppose you had a 3 x 12 uint8 matrix, and wanted to typecast it to uint32: what would you want that to mean? What shape would you expect the result to be? There is the right amount of data there to make it in to 9 uint32 but 3 x 3 would surely be the wrong shape for the output, considering that the 4th byte of the first uint32 would have to be taken from the top row of the second column of the input array.
About the only way that would make sense to me to more or less preserve the dimensions would be if, when a type was being typecast to another type, the first dimension of the output would be adjusted so as the occupy the same number of bytes per column as the original first dimension did (getting bigger if the new type used less room, getting smaller if the new type occupied more room), with it being an error if the first column could not be converted to an integral number of items in the new type.
If that is the rule you would use, it would not be difficult to implement it. Keep in mind that reshape() and (:) do not move data around, just put a new header on it, so the operations would be fast:
function NewArray = typecastarray(TheArray, NewType)
curshape = num2cell(size(TheArray));
NewArray = reshape( typecast(TheArray(:),NewType), [], curshape{2:end});
end
  댓글 수: 1
VINAYAK KARANDIKAR
VINAYAK KARANDIKAR 2019년 1월 9일
This does not work for audio data. For example an array(2 channel) audio file of char type, when type-casted into a uint8 array(becasue sound and wavplay command do not accept data types of char type), and if the audio is played, we hear garbage, that's why!!!

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

추가 답변 (1개)

Carl Witthoft
Carl Witthoft 2020년 3월 30일
If you know a little bit about your 2D array in advance, then try this - seemed to work for me
Here I know my columns are 4*N bytes long, and that I want to typecast column-wise
arrsize = size(arrdat);
f32 = zeros(arrsize(1)/4, arrsize(2) );
for jrow = 1:4:size(arrdat,1),
count4 = jrow:jrow+3;
rowquad = arrdat(count4,:);
f32((1+(jrow-1)/4),:) = double(typecast(rowquad(:), 'single'));
end

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by