Copy elements in a structure or a matrix?
古いコメントを表示
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.
回答 (1 件)
Benjamin Thompson
2022 年 3 月 22 日
0 投票
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 件のコメント
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)
"but it is harder to copy groups of elements from one cell array to another"
The indexing used to copy elements of any array and assign to another array are exactly the same for all array classes. For all array classes parentheses refers to the specified elements of the array, and this applies to cell arrays as it does to structure arrays. There is no difference.
Bárbara Matos
2022 年 3 月 24 日
Benjamin Thompson
2022 年 3 月 24 日
See the documentation article titled "Structures". Have you tried using setfield and orderfields to help rearrange your structure? I am still not clear what you are trying to do so what you should do is post some code showing the way you can change your structure in a slow manner. Then ask the community if there is a faster implementation of your example.
Bárbara Matos
2022 年 3 月 24 日
Stephen23
2022 年 3 月 24 日
@Bárbara Matos: is that the same task that you asked about in your earlier question?:
Bárbara Matos
2022 年 4 月 19 日
カテゴリ
ヘルプ センター および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!