read data from file and convert to a 3d array.
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
So, I have a dat file storing a 3d data (131*385*97), the data is store in form below:
a(1,1,1),a(1,1,2)...........a(1,1,97)
a(1,2,1),a(1,2,2)...........a(1,2,97)
. . . . . . . . . . .
a(1,385,1),a(1,385,2)...........a(1,385,97)
a(2,1,1),a(2,1,2)...........a(2,1,97)
. . . . . . . . . . .
a(131,385,1),a(132,385,2)...........a(131,385,97)
My question is how I read this form of data and store it as a 3d array in matlab?
0 Kommentare
Antworten (2)
Abhishek Kumar
am 8 Jul. 2019
Bearbeitet: Abhishek Kumar
am 8 Jul. 2019
I am attaching the code below this works for data (3*3*3), make changes accordingly to suit your purpose.
Also, If you wish to read data from a dat file, you will have to use the function importdata(..)
for more information read the documentation or try
>> help importdata
a = reshape(1:27, 3, 9) .'; % here i have data which is 9*3
i=1;
m=1;
n=1;
o=1;
for j =1:9
o=1;
for k=1:3
A(m, n, o)=a(j,k);
o=o+1;
if mod(j, 3)==0 && i~=3 && k==3
o=0;
n=0;
m=m+1;
end
end
n=n+1;
end
0 Kommentare
Rik
am 8 Jul. 2019
Bearbeitet: Rik
am 8 Jul. 2019
To suggest the best way to read the file, you will have to share a small sample. Otherwise functions like importdata or the more low-level textscan will probably do the job.
For the parsing of the data once you have read the file, you can use the code below. As you can see with the check at the end, the output is as expected. There is no way to automatically determine how many rows and columns there are, so you will have to provide at least one of the two. The number of pages is simply the second dimension of your input, and once you have rows or cols you can divide the length of the second input by the one to obtain the other.
cols=3;rows=4;pages=5;
data=reshape(1:cols*rows*pages,cols*rows,pages);%generate random data
%each cell is the third dimension
b=mat2cell(data,ones(1,size(data,1)),size(data,2));
b2=reshape(b,rows,cols)';
flip_to_third_dim=@(x) permute(x(:),[3 2 1]);
out=cellfun(flip_to_third_dim,b2,'UniformOutput',false);
out=cell2mat(out);
%check:
isequal(out(1,1,:),flip_to_third_dim(data(1,:)))
0 Kommentare
Siehe auch
Kategorien
Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!