converting multiple file of 4D to 3D files

1 Ansicht (letzte 30 Tage)
Hanisah noh
Hanisah noh am 3 Jul. 2020
Kommentiert: Hanisah noh am 10 Jul. 2020
i have 80 files of 4D double (140x122x180x#)
i need the files in 3D (140x122x180)
i have tried these solutions but does not come out as expected
files = B % 140x122x180x#
threeD = permute(files[1,2,3,4])
% the result will be 4D double
% i also tried
threeD = squeeze(num2cell(permute(files,[1,2,3,4]),1:3))
% the result will be #x1 cells
current coding :
cd ('path');
files=dir('*.mat'); % find all |.mat| files in the current directory.
totalfile = numel(files); % count those files.
C = cell(1,totalfile); % preallocate a cell array for the loaded data.
for a = 1:totalfile % loop over the number of files.
T = load(files(a).name) % load the data from each file
C(a) = struct2cell(T); % convert structure to cell, allocate to cell array.
M = cat(4,C{:}) % concatenate all MxN matrices into MxNxP array,
% 3 dimensional concatenate
% ???
save(['path/zscore_' num2str(a) '.mat'],'M');
end
thank you
  4 Kommentare
Rik
Rik am 3 Jul. 2020
I don't know what your variables mean, they're your variables.
The 2 is just an example, as you didn't secribe what value # should have. You wanted a length of 1 in the fourth dimension, that is what this code does. It selects the second book. (in general the first 2 dimensions of an array are called rows and columns, the third is often called page, and some people call the fourth dimension book (i.e. a collection of pages))
Hanisah noh
Hanisah noh am 3 Jul. 2020
i see. thanks ! big help

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Vinai Datta Thatiparthi
Vinai Datta Thatiparthi am 5 Jul. 2020
Hey Hanisah,
As mentioned by @Rik, iterate through the collection of 4D matrices to get your 3D values.
For example,
for i = 1:size(B, 4)
A = B(:,:,:,i) % Simple example to slice your 4D matrices
end
Hope this helps!

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by