How to define a struct array with length more than one and assign values to one of the strucy array?

Dear All,
I have a real array A. I want to define a struct array S to save the colculation reslut of element in A to the corresponding struct.
For example, A = [1 0.2 3 0.4 5 6]. I want to define strucy array S (with length 6) to save the calaulation result of elements in A.
S(1) = struct('A.1', A(1), 'Result', A(1)^2);
S(3) = struct('A.3', A(3), 'Result', A(3)^2);
S(4) = struct('A.4', A(4), 'Result', A(4)^2);
S(6) = struct('A.6', A(6), 'Result', A(6)^2);
I only want to save these 4 values.
My question is:
1). How to define S?
2). How to assign values to S?
Thanks a lot.
Benson

 채택된 답변

Stephen23
Stephen23 2020년 10월 12일
편집: Stephen23 2020년 10월 12일
>> A = [1,0.2,3,0.4,5,6];
>> X = [1,3,4,6]; % "I only want to save these four values"
>> Ac = num2cell(A);
>> Bc = num2cell(A.^2);
>> S = repmat(struct(),1,6);
>> [S(X).A] = Ac{X};
>> [S(X).B] = Bc{X};
>> S.A
ans = 1
ans = []
ans = 3
ans = 0.40000
ans = []
ans = 6
>> S.B
ans = 1
ans = []
ans = 9
ans = 0.16000
ans = []
ans = 36

댓글 수: 4

Thank you both for the great help.
I found my issue is more complex than I described originally. The values corresponding to the elements in A has more than 1 iterms, its dimension is not fixed in advance. For example, A = [1 0.2 3 0.4 5 6]. I want to define strucy array S (with length 6) to save some values in a struct with the same length of A. The values could be:
For A=1, we save [3 1]
For A=3, we save [-2]
For A=4, we save [5 10 23.0]
For A=6, we save [-7 2 11 38]
We need to use a struct to save those values because their dimension is not fixed. We want to have a struct like below:
S.A =
ans = 1
ans = []
ans = 3
ans = 0.40000
ans = []
ans = 6
A.Values =
ans = [3 1]
ans = []
ans = [-2]
ans = [5 10 23.0]
ans = []
ans = [-7 2 11 38]
The values are not directly ralted to the elements of A. But we use A is because we need to save those values corresponding to the elements in A. How to deal with this issue?
Thanks a lot again.
Benson
>> A = [1,0.2,3,0.4,5,6];
>> X = [1,3,4,6];
>> C = {[3,1],-2,[5,10,23],[-7,2,11,38]};
>> S = repmat(struct(),1,6);
>> Ac = num2cell(A);
>> [S(X).A] = Ac{X};
>> [S(X).Values] = C{:};
>> S.A
ans = 1
ans = []
ans = 3
ans = 0.40000
ans = []
ans = 6
>> S.Values
ans = 3 1
ans = []
ans = -2
ans = 5 10 23
ans = []
ans = -7 2 11 38
Are you required to store your data in a struct array, or would a cell array be acceptable?
c = cell(1, 10);
for k = 1:10
c{k} = magic(k);
end
celldisp(c)
Thanks, Stephen, It works well.
Benson

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

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 10월 12일
편집: Steven Lord 2020년 10월 12일
A.1 is not a valid struct field name.
With something this simple, I probably wouldn't use a struct array.
A = [1 0.2 3 0.4 5 6]
Asquared = A.^2
If you wanted to store these in one array rather than two, since the result of the operation is the same type and size as the data on which you're operating, you can store them as rows of the array:
B = [A; Asquared]
or as a table array:
C = table(A.', Asquared.', 'VariableNames', ["A", "A^2"]) % If you're using R2019b or later
C = table(A.', Asquared.', 'VariableNames', ["A", "A_squared"]) % R2019a or earlier
See the Release Notes for more information about the change in release R2019b.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

질문:

2020년 10월 12일

댓글:

2020년 10월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by