MATLAB Answers

Creating a multi-dimensional array out of many lower-dimensional arrays

2 views (last 30 days)
Ayden Clay
Ayden Clay on 3 Apr 2020
Edited: Ayden Clay on 6 Apr 2020
I currently have a hundred or so large datasets that are MxN (thus 2-D), for the external program that I use to gather this information I must run it for each of the parameters that I'm changing, for example ... etc. Therefore I now have hundreds of MxN datasets that need to be combined into a multi-dimensional array. To give an example:
What I currently have:
data(alpha,mach) = 15; % element of the data at specified alpha and mach, for delta1 = 0; delta2 = 0; delta3 = 0; delta4 = 0;...
data(alpha,mach) = 10; % element of the data at specified alpha and mach, for delta1 = 5; delta2 = 0; delta3 = 0; delta4 = 0;...
Now I would like to have
data(alpha,mach,0,0,0,0,...) = 15;
data(alpha,mach,5,0,0,0,...) = 10;
I've looked into the "cat" command, but I'm not 100% on how it works, and how to check that the data that I've combined retains the same information.
Any help would be greatly appreciated!


Show 6 older comments
Matt J
Matt J on 4 Apr 2020
I'm sorry, but your Maltlab syntax looks entirely made-up, making it hard to understand how your data is held. For example "data.6" is not a name that Matlab would allow you to give to any variable type. My suggestion would be that you re-write the code that created your data so that as it loops through the different delta combinations, it stores results to an N-D array directly. That's what you will want to do anyway for future runs.
Ayden Clay
Ayden Clay on 4 Apr 2020
Sadly, the program that I use is external and produces data in an unchangeable way, the data is stored in data.for006{1:end} sorry for my confusion, I misstyped.
Taiwo Bamigboye
Taiwo Bamigboye on 4 Apr 2020
Have tried so hard to learn MATLAB on my own but it seems not working for me. Please is there anyone that knows it very well and ready to teach me via zoom or team view, off course am ready to pay. All this online learning is not intuitive for me.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 4 Apr 2020
Edited: Matt J on 4 Apr 2020
If your "datasets" really are in the form of the Matlab dataset type described here, then I think you could probably just do something like the following:
C=dataset2cell(data); %convert the datasets to cell array
[m,n,p,q,r,s,t]=deal(10,12,5,5,5,5,5); %The data dimensions
dataArray = reshape( cat(3,C{:}) , [m,n,p,q,r,s,t]);
Now, for interpolation purposes, you can create a griddedInterpolant object as below,
gridVectors ={alphaList,machList,delta1List,....}; %List of sample grid values for each of the variables


Show 4 older comments
Ayden Clay
Ayden Clay on 6 Apr 2020
I have since solved this issue finally using the permute command to correctly organize the data. Sadly, this comes with the requirement that the number of betas and the number of machs are the same. I'm working on fixing this, but the working code is now:
S = [data.for006{:}];
numrecov = cat(3,; = permute(reshape(numrecov,[length(alpha),length(beta),length(mach),length(delta1),length(delta2),length(delta3),length(delta4)]),[1 3 2 4:length(RANGES{1})]);
Which works for length(beta)==length(mach). I'm yet to find a solution that doesn't rely on that process.
Matt J
Matt J on 6 Apr 2020
I don't see anything in your code that relies on length(beta)==length(mach).
Ayden Clay
Ayden Clay on 6 Apr 2020
It seems to be the case that the reshaped array will be size = [nalpha,nbeta,nmach,ndelta1,...]; Which, must be permuted (swapping the mach and beta dimensions) to produce the correct array. So, nmach == nbeta.
I wish it weren't the case, if the reshape method or the cat method can immeditely produce the correct array that would be perfect.

Sign in to comment.

More Answers (0)

Sign in to answer this question.