2

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?

1
  • What's does your expected output "normal array" look like? One wide 2 x (n_1 + n_2 + ... + n_N) array? It would help if you edited your question to make the code into a minimal reproducible example we can actually run using arbitrary values (e.g. using rand()) since we don't have your mat files, then you can illustrate the expected output Commented Jul 18, 2024 at 7:06

2 Answers 2

1

(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.';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. First time Matlab. Your answer was Helpful. i was stuck with the input in my next step being a 2 x n x m array. array did not store variable n valued dim.
0

command cell2mat does exactly what you ask.

Check it here

https://uk.mathworks.com/help/matlab/ref/cell2mat.html?s_tid=srchtitle_site_search_3_cell2array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.