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 个)

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 = struct with fields:
X: 1 Y: 2 Z: 3
A(2,1) = struct('X', 4, 'Y', 5, 'Z', 6)
A = 2×1 struct array with fields:
X Y Z
A(2,2) = struct('X', 7, 'Y', 7, 'Z', 7)
A = 2×2 struct array with fields:
X Y Z
A(1,2) = struct('X', 4, 'Y', 5, 'Z', 6)
A = 2×2 struct array with fields:
X Y Z
B = A
B = 2×2 struct array with fields:
X Y Z
B(3,1) = 0
Assignment between unlike types is not allowed.
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.
Thank you both! Actually, I understand what you are saying, but I think that wasn't my problem. Maybe I didn't explain well.
So I have an antenna array in figure 1. When I choose the number os lines and columns a struct is created, with all the combinations of possible antennas. In figure 2 I have the struct and in figure 3 the code.
And when I click in the S parameters button, I get the relations between antennas from some files and put them into the struct (see figure 4 and 5 for the struct and the code).
The problem is: if I change the number of columns and lines, the struct changes and I need more relations. What I want is to find a way to copy the parameters s11, s12, s21 and s22 which relate antenna 1,1 with 1,2 for the relation of 1,2 with 1,3. And the parameters which relate 1,1 with 2,2 for the relation of 1,2 with 2,3. And so on... (to understand the relations see figure 6, the part of the array). Actually I want to copy the relations of the antennas 1,1, 1,2, 2,1 and 2,2 for the rest of the array.
I can't discover a good way to do this. Because if I want to check all the numbers of the struct, I need 4 for cicles do run all the array and another 4 for cicles to the antennas 1,1 until 2,2. (figure 7). I was trying to know if you have some suggestion on this. Some function I don't know, or a better way to do this.
I hope you have understand and thank you a lot!
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.
Thank you so much. I will look better in the documentation and if I don't find anything, I'll ask the comunity with the code. Thank you!
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by