Subtracting in a structure

16 Ansichten (letzte 30 Tage)
Sam Broad
Sam Broad am 21 Nov. 2021
Beantwortet: Image Analyst am 21 Nov. 2021
Hi, Sorry if some stuff doesnt make sense as I'm quite new to matlab!I have 1 x 229 struct array with 13 fields. One of the fields is a total number which adds up consecutively from a count.
Pass(NumPass).TotalPlayers = Count3
I'm trying to find a way to minus these numbers from each other so the difference is added to a SortedTotalPlayers field, so if the first 5 numbers in the TotalPlayers field were: 6,10,12,16,26, I would want the first 5 numbers in the SortedTotalPlayers field to be 6,4,2,4,10.
Pass(NumPass).SortedTotalPlayers = ?
I haven't realy got a clue what to put after the = sign, is this possible?
Thanks

Antworten (3)

dpb
dpb am 21 Nov. 2021
Pass(NumPass).SortedTotalPlayers = diff[0 Pass(NumPass).SortedTotalPlayers];

Stephen23
Stephen23 am 21 Nov. 2021
Bearbeitet: Stephen23 am 21 Nov. 2021
You can do all of them at once using some comma-separated lists:
S(1).F = 6;
S(2).F = 10;
S(3).F = 12;
S(4).F = 16;
S(5).F = 26;
S.F % checking
ans = 6
ans = 10
ans = 12
ans = 16
ans = 26
C = num2cell(diff([0,S.F]));
[S.F] = C{:};
S.F % checking
ans = 6
ans = 4
ans = 2
ans = 4
ans = 10

Image Analyst
Image Analyst am 21 Nov. 2021
Try this:
NumPass = 1;
Pass(NumPass).TotalPlayers = [6,10,12,16,26];
firstNumber = Pass(NumPass).TotalPlayers(1); % Get the "6"
Pass(NumPass).SortedTotalPlayers = [firstNumber, diff(Pass(NumPass).TotalPlayers)]
Pass = struct with fields:
TotalPlayers: [6 10 12 16 26] SortedTotalPlayers: [6 4 2 4 10]

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by