Concatenation of 3D Matrices

4 views (last 30 days)
h_chep
h_chep on 6 Aug 2020
Commented: Arthur Roué on 6 Aug 2020
Greetings!
I would like to find a solution to a bottleneck that I've encountered. So I have a 3D array of the 3x3x644 size, consisting of 644 uniques 3x3 sub-matrices. The whole array represents 644 seconds and state information for each timestep is represented by those 3x3 sub-matrices. My goal is to create such rouitine that would allow me create an artificial delay for a consideration, meaning that I'd like to test how system system behaves with a state information delay of 2, 4, etc seconds. This implies that even though I have current system state information for every second, I would need to create a dataset where system state information is updated every 2,4,etc seconds. For instance, if to consider a delay of 2 seconds, I need every two matrices of that 3x3x644 array be the same, or in a case of 4 seconds - every four matrices shall be duplicated.
Here is partial routine I've came up with, but I am struggling with the last step. I am pretty sure that there is a way easier and elegant solution to my problem, but I cannot figure it out. Thank you for any suggestions!
r=2; %'update rate'
n=644; %total number of entries
A=rand(3,3,n); %creating 3x3x644 3D-matrix
A1=A(:,:,1:r:end); %getting rid of every n-th matrix (where every r-th matrix is the one to be updated) with 'previous' matrix, resulting in a 3x3x322 matrix
A2=repmat(A1,1,2); %duplicating 3x3 matrix in all of 322 entries, resulting in 3x6x322 matrix
for i=1:size(A2,3) %i=1:332
B=A2(:,:,i); %localizing 3x6 i-th matrix
B1=B(:,1:3); %splitting 3x6 i-th matrix into two separate 3x3 clone matrices
B2=B(:,4:end);
%C1(:,:,i)=cat(3,B1,B2); %concatinating splitted 3x3 clone matrices into 3x3x2 3d matrix
%Here I need some routine that would stack 3x3x2 matrices C1
%onto each other resulting in matrix of original dimentionality of
%3x3x644
end

Accepted Answer

Arthur Roué
Arthur Roué on 6 Aug 2020
This should do the trick
r = 2; %'update rate'
n = 644; %total number of entries
A = rand(3,3,n); %creating 3x3x644 3D-matrix
for idx = 1:r:n
A(:,:,idx+(0:r-1)) = repmat(A(:,:,idx), 1, 1, r);
end
  2 Comments
h_chep
h_chep on 6 Aug 2020
Thank you, Arthur!
Very elegant and quick solution!
Arthur Roué
Arthur Roué on 6 Aug 2020
You're welcome ! ;)

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!