Why create a 1-by-3 structure array with one field by this way?
조회 수: 21 (최근 30일)
이전 댓글 표시
댓글 수: 0
채택된 답변
Stephen23
2023년 7월 22일
편집: Stephen23
2023년 7월 22일
"Is it better than this"
Not really: both are fragile code and should be avoided.
They will both throw errors or produce unexpected results when that variable already exists in the workspace (e.g. of another class, with another size). However implicitly defining the entire array using indexing is likely to be faster.
But the recommended, easy, and robust approach to preallocate the entire structure array would be:
S = struct('a',cell(1,3))
"Why create a 1-by-3 structure array with one field by this way"
Because it looks simpler and is less likely to scare beginners.
Because examples given in the documentation are supposed to show how things work, but are not written in terms of best code practices.
Because TMW staff are human like the rest of us.
댓글 수: 4
Stephen23
2023년 7월 22일
편집: Stephen23
2023년 7월 22일
"s(3).a=[] could produce what unexpected?"
Probably when you looked at that code in the DEAL documentation you expected that the resulting structure has three elements in it. But as I wrote, if a structure of that name already exists then that code can have an unexpected output:
S = struct('x',num2cell(magic(4)));
% ... lots of code here
S(3).a = [] % not 3 element structure
New users regularly do this kind of thing: some of them expect and want this, others do not expect this because they are only partly aware of what their own code is doing or what certain operations actually do. We regularly respond to questions on this forum that are caused by users not knowing/forgetting/not checking the sizes, classes, or existence of variables in their workspaces.
Thus unexpected by certain users.
추가 답변 (1개)
Manan Jain
2023년 7월 22일
Hi!
Let's discuss the differences between the approaches:
- S.a = []; This approach creates a single field a in the structure S and assigns an empty array to it. The result is that all elements of S will share the same a field.
- S(2).a = []; This approach creates an array of structures S, and then assigns an empty array to the a field of the 2nd structure. The other structures still have an empty a field because it wasn't explicitly assigned for them.
- S(3).a = []; This approach is similar to the previous one, but it assigns an empty array to the a field of the 3rd structure.
So, the main difference between the two approaches is how they handle the field a in the structures:
- In the first approach (S.a = []), all structures in the array S share the same field a.
- In the second approach (S(2).a = []), only the 2nd structure has the field a.
- In the third approach (S(3).a = []), only the 3rd structure has the field a.
Which approach is better depends on your specific use case and requirements. Here is what you can look while deciding:
- If you need all structures to have the same field a with the same value, then the first approach (S.a = [];) is more appropriate.
- If you have specific elements in the array S that require the field a, then the second or third approach (S(2).a = []; or S(3).a = [];) can be used to assign the field only to those specific elements.
I hope this helps!
Thanks
댓글 수: 6
Bruno Luong
2023년 7월 22일
편집: Bruno Luong
2023년 7월 22일
Anotherway to allocate array of structure is
S = repmat(struct('a',[]), 1, 3)
% But this will return an unexpected result for some
S = repmat(struct('a',{}), 1, 3)
Stephen23
2023년 7월 22일
편집: Stephen23
2023년 7월 22일
"teacher said "if you know the structure size is n,you should creat S(n).xx=[]. It will make your programe run fast,because it will Pre requested memory""
They are certainly right that a long time ago, this approach was blazingly fast for preallocating numeric arrays. Whether it still is today, I have not checked (but given the significant changes to the JIT engine etc. I would not presume this to be still true or significant, without further testing).
In any case, preallocating numeric arrays is much more robust with ZEROS, NAN, etc.
The code I gave you here also preallocates the entire structure array, with the added benefit that it is also much more robust as well. Perhaps your teacher did not really understand how to write robust and efficient code.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!