How to Convert 4D-Single to MxNXN

Dear all,
I have 100 set data 4D-Single. anyone know how to convert it to MxNxN?
Letsay my 4D-Single is cropVol, want to convert to g (see the picture in workspace)

21 Comments

What is the dimension and class of 4D?
Can the number of elements in your 4-D arrays be factored into NxNxM? Or do you need to discard or extend some elements of the 4-D array, or do you want to only fill some part of the NxNxM array?
This all 400 data set that I want to change into MxNxN
Because the type .mat is 4D-Single
The size of data in each file is same?
To what size you want ?
to each size itself
Or you have any idea, how to read the data for 4D-Single for volds?
clc
clear all
close all
%testDataimages
DATASetDir = fullfile('C:\Users\USER\Downloads\BrainTS\preprocessedDataset');
IMAGEDir = fullfile(DATASetDir,'imagesTr');
volReader = @(x) matRead(x);
volds = imageDatastore(IMAGEDir, ...
'FileExtensions','.mat','ReadFcn',volReader); %THIS ONE IS DATA FOR MXNXN
% labelReader = @(x) matread(x);
matFileDir = fullfile('C:\Users\USER\Downloads\BrainTS\preprocessedDataset\labelsTr');
classNames = ["background", "tumor"];
pixelLabelID = [0 1];
% pxds = (LabelDirr,classNames,pixelLabelID, ...
% 'FileExtensions','.mat','ReadFcn',labelReader);
pxds = pixelLabelDatastore(matFileDir,classNames,pixelLabelID, ...
'FileExtensions','.mat','ReadFcn',@matRead);
@mohd akmal masud: You will have to explain what size your 4-D array has, what size you want to make the 3-D version, and what elements should be included (or excluded). Otherwise someone might just suggest that you take the first 12 data-points and put it into a 2x2x3 array:
NbyNbyM = reshape(cropVol(1:12),[2 2 3]);
Let's make this a little more understandable. Let's say you have a 2D single and want to convert this to a 1D double. How would you do that?
And what is the end purpose of this conversion?
ok, lets me explain.
actually, I have 400 data set brain tumor. all the data set have the various x,y,z size.
Means all the data set have different x,y,z size.
all my data set is 4D-Single.
Because its all 4D-Single, then I cannot view in volume segmenter apps or volume viewer apps.
then need to convert all data set to x y z single.
are you all get my points?
@Bjorn Gustavsson this one is corect.
But how to loop for 400 data set, then I want o reshap it into the origional size for all?
x-y-z is only 3 dimensions. Where are you getting the fourth? Is it maybe the case that your dimensions are x-y-color_channel-z (like the example below)?
load mri
size(D)
ans = 1×4
128 128 1 27
If so, permuting the color channel to the 4th position should solve the problem.
new_D=permute(D,[1 2 4 3]);
size(new_D)
ans = 1×3
128 128 27
ok let me explain again.
let say one of my data is LiverTS001.mat
then I drag it into command window
>>load('LiverTS001.mat')
so the workspace will be
then I used squeeze to convert it to MxNxN
>>File = squeeze(cropVol);
then the worjspace will be
SHOULD BE the value File is something like 256x256x89 (the size data itself)
The squeeze function will only remove dimensions of length 1. It does not promise to make your array 3D. Apparently, none of the dimensions of cropVol have a unit length. You can use the size function to show you the actual size.
@mohd akmal masud: Give us this information we have been pleading for multiple times:
>> size(cropVol)
Then tell us what the different dimensions correspond to (first dimension might be the variation in x-position for example, but until you've told us that would be pure conjecture), which components you want to display which you want to discard etc. That the array is 4-D tells us very little about what anyting of it means. Is one of the dimensions time? Or wavelength of the X-rays if this is the output from an X-ray tomographic imaging system.
Do any of those numbers mean something to you? Do you expect 4 of something? Is your detector 176 by 224?
Without knowing what your dimensions actually mean it is not possible to help you reduce the dimension count to 3.
To explore, you could use the volume viewer on cropVol(:,:,:,k) (putting each of the 4 in a subplot). I don't know if that will help at all, since you're being very sparse with explanations.
When the 4-D array-size is [176 224 152 4] the only way to put your 4-D array into an N-by-N-by-M array, as you requested is to put the elements into an 128-by-128-by-1463 array. You can do that by something like:
A3D = reshape(cropVol,[128 128 1463]);
If you want or need to re-arrange the for dimensions (of still unknown meaning to us since you still will not tell us what they signify.) you can do that with the permute function. This is the only way to put all elements of an array with dimensions [176 224 152 4] into an N-by-N-by-M array, since the total number of elements factor into: 2^14 * 7 * 11 * 19.

Sign in to comment.

Answers (0)

Categories

Products

Release

R2022a

Asked:

on 30 Jun 2022

Community Treasure Hunt

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

Start Hunting!