Concatenation of 3D Matrices

1 次查看(过去 30 天)
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

采纳的回答

Arthur Roué
Arthur Roué 2020-8-6
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 个评论
h_chep
h_chep 2020-8-6
Thank you, Arthur!
Very elegant and quick solution!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by