Typecasting higher dimension matrices
Show older comments
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
on 21 Sep 2011
Hello,
What do you mean?
A=[2,2;1,3];
B=single(A);
seems to work.
Regards,
Florin
Sean
on 21 Sep 2011
Accepted Answer
More Answers (1)
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
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!