MATLAB Answers

Typecasting higher dimension matrices

66 views (last 30 days)
Sean
Sean on 21 Sep 2011
Answered: Carl Witthoft on 30 Mar 2020
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 Comments

Florin Neacsu
Florin Neacsu on 21 Sep 2011
Hello,
What do you mean?
A=[2,2;1,3];
B=single(A);
seems to work.
Regards,
Florin
Sean
Sean on 21 Sep 2011
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.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Sep 2011
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 Comment

VINAYAK KARANDIKAR
VINAYAK KARANDIKAR on 9 Jan 2019
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!!!

Sign in to comment.

More Answers (1)

Carl Witthoft
Carl Witthoft on 30 Mar 2020
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

  0 Comments

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!