MATLAB Answers

Typecasting higher dimension matrices

47 ビュー (過去 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

  0 件のコメント

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by