I have M
many 2-D
matrices of dimension (2 x n_1 ,2 x n_2 , 2 x n_3,..., 2 x n_N )
. I can store them in a MATLAB cell-array of dim (2 x N x M)
where N = {n_1 , n_2 , n_3,..., n_N}
and n_1
,n_2
,n_3
are all distinct, not-equal.
arraydata = {}
for i = 1:M
file_path = fullfile(basepath, samplenames{i}, 'temp.mat');
% Load the data from the constructed file path
loaded_data = load(file_path);
temp = loaded_data.mymatrix; # saved mtx are of dim n_i x 2
data1 = transpose(temp); #take transpose
arraydata{i} = data1;
end
How to convert this into a normal array?
(In the original version of the post) You are assigning n_i x 2 values into a single element of the output array.
There are two ways to concatenate all the arrays, one is much more performant than the other.
The better performing version uses the cell array to first load all the arrays, as you do in the revised question:
arraydata = cell(1,M); % preallocate the array!
for i = 1:M
file_path = fullfile(basepath, samplenames{i}, 'temp.mat');
% Load the data from the constructed file path
loaded_data = load(file_path);
data1 = loaded_data.mymatrix.'; # saved mtx are of dim n_i x 2, transposed to 2 x n_i
arraydata{i} = data1;
end
arraydata = [arraydata{:}]; % concatenate horizontally
The other, simpler but much slower way is:
arraydata = [];
for i = 1:M
file_path = fullfile(basepath, samplenames{i}, 'temp.mat');
% Load the data from the constructed file path
loaded_data = load(file_path);
data1 = loaded_data.mymatrix.'; # saved mtx are of dim n_i x 2, transposed to 2 x n_i
arraydata = [arraydata,data1]; % concatenate horizontally
end
Note that you could also load the data a bit more simply as load(file_path,'mymatrix')
, without assigning to anything, which will create the variable mymatrix
. Specifying which array to load in this way can save a lot of time if the MAT-files contain more data than the array you want to load.
In recent versions of MATLAB you can also apply dot indexing directly on the function call:
data1 = load(file_path,'mymatrix').mymatrix.';