# Creating a 3D matrix by combination of a 3D matrix and a 2D matrix

4 views (last 30 days)
Francis Chabot on 18 Aug 2021
Commented: the cyclist on 19 Aug 2021
Hello community,
I'm trying to add data from a 2D matrix to an existing 3D matrix in my code.
My 3D matrix is 125x2x20 (125: # of the companies, 2: firms id and data, 20: # of years of data)
My 2D matrix is 2784x21 (2784: # of the companies, 21: firms id and 20 years of data)
Here's what I'm trying to do:
The number of row (125 for 3D and 2784 for 2D) represents the numbers of the companies ranging for 1 to 2784. In my 3D matrix I've narrow them to only the ones that I need and they are in a random order (ascending order of the data in column 2 for the 20 years).
My goal is to be able to identify the companies in the 2D matrix that are in the 3D one to add a colum in my 3D matrix which will end up in a 125x3x20. As you can understand, the 20 years of data in my 2D matrix correspond to the 20 years which represent the 3rd dimension of my 3D matrix. So, for each years, I would add a 3rd column in the 3D matrix that represent a different statistic.
Thanks for your help,
Best regards,
##### 1 CommentShow -1 older commentsHide -1 older comments
darova on 18 Aug 2021
Try ismember

Sign in to comment.

### Answers (1)

the cyclist on 18 Aug 2021
I think this does what you want:
% Set seed for reproducibility of this example
rng(55)
% Made-up data of the correct size
M3 = rand(125,2,20); % (125: # of the companies, 2: firms id and data, 20: # of years of data)
M2 = rand(2784,21);
% Create company ids that will match across the two arrays
M2(:,1) = randperm(2784);
M3(:,1,1) = randperm(125);
% Everything above this line is just making up some data.
% Everything below is the matching algorithm.
% Match up the corresponding companies across the two arrays
[hasMatch,indexToCompanyMatch] = ismember(M2(:,1),M3(:,1,1));
% Pull the matched company data from M2, and reshape to append to M3
matchedData = reshape(M2(hasMatch,2:end),125,1,20);
% Append matching data, ordered correctly
M3(indexToCompanyMatch(hasMatch),3,:) = matchedData;
##### 2 CommentsShow NoneHide None
Francis Chabot on 19 Aug 2021
Hi, when I tried it I received this error code :
>> matchedData = reshape(r_yearnco(hasMatch,2:end),125,1,20);
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
And in the first line of code (matchedData...) when matching the 2 arrays you're only selecting the first year
M3(:,1,1) instead of M3(:,1,20) and I was wondering if that's normal because what I'm trying to do is to attribute the data in the 2D matrix for the 20 years so that the final matrix would be of dimension 125x3x20
Thanks for the help,
the cyclist on 19 Aug 2021
OK, so I have a few comments here.
First, I assume that my example code worked for you, before you tried to apply it to your own data. I would say that the most important thing for you to do is to understand how my example code is working (not simply get it to run), so that you can adapt it to your data.
I have a theory why my code is not working as written, with your data. One is that I assumed that every company represented in M3 is also present in M2. If that is not the case (or if I am somehow doing the matching wrong), then you will get that size mismatch error. Another possibility, especially given your next statement, is that I simply got the matching algorithm wrong.
The reason I matched on M3(:,1,1)is that you said that the 2nd dimension was [firmid,data]. I took that to mean that the first "slice" along the second dimension is the firm id and that the second slice is data. Therefore I (tried to) match firm id from M3 to the firm id of M2.
Probably if you fix up the matching that I got wrong, you'll solve it.

Sign in to comment.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!