Concatenation of 3D Matrices

조회 수: 1 (최근 30일)
h_chep
h_chep 2020년 8월 6일
댓글: Arthur Roué 2020년 8월 6일
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!
Arthur Roué
Arthur Roué 2020년 8월 6일
You're welcome ! ;)

댓글을 달려면 로그인하십시오.

추가 답변 (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