Copy elements in a structure or a matrix?

5 views (last 30 days)
Bárbara Matos
Bárbara Matos on 22 Mar 2022
Commented: Bárbara Matos on 19 Apr 2022
I have a matrix 2x2 presented in figure 1. For that matrix, I have a structure with all the relations. For each relation there is a parameter s11, s12, s21 and s22. (See figure 2).
When I chose an higher matrix such as 6x6, I want to copy the relationship of the 2x2 matrix for the rest fo the matrix. For example: I want to copy the relation of 1,1-2,2 for the rest of the matrix (1,2-2,3 and 1,3-2,4 and 1,4-2,5, ..., and 2,1-3,2 and ... and 5,1-6,2, etc). And the same with the other relations. (See figure 3 to understand better the relations). Those new relations will be in the same structure (figure 4).
I would like to know if there is a function or something I can use which can make this task of copying everything easier.
Because the final goal is that the user can chose the matrix he want to copy. So in this example, I want to copy the 2x2 matriz for the rest. But after this, I could chose copy the matrix 3x3 or 4x4 instead.
I know there are many functions I don't know. If you can suggest something, I would appreciate it! Thank you.

Answers (1)

Benjamin Thompson
Benjamin Thompson on 22 Mar 2022
Here are some sample commands for creating and copying structure arrrays:
A(1,1) = struct('X', 1, 'Y', 2, 'Z', 3)
A(2,1) = struct('X', 4, 'Y', 5, 'Z', 6)
A(2,2) = struct('X', 7, 'Y', 7, 'Z', 7)
A(1,2) = struct('X', 4, 'Y', 5, 'Z', 6)
B = A
B(3,1) = 0
B(3,1:3) = struct('X', 0, 'Y', 0', 'Z', 0);
B
B(3,:)
B(3,1:3)
B(3,3)
B(3,2)
C = struct('X', 0, 'Y', 0, 'Z', 0)
C(1:3,1:3) = C
C(1:2,1:2) = A
C(1,2)
C(1,1)
You can also use cell arrays but it is harder to copy groups of elements from one cell array to another.
  6 Comments
Bárbara Matos
Bárbara Matos on 19 Apr 2022
No, it is not the same. I understood how to solve the problem in the previous question. This one is difficult to explain, but I am working on it! Thank you very much.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!