Azzera filtri
Azzera filtri

Klassieren und Zählen der Werte eines Arrays || Classifying and counting the values of an array

38 visualizzazioni (ultimi 30 giorni)
Hallo zusammen,
ich möchte die EInträge eines Arrays klassieren und zählen. Ich habe ein 10x2 array, welches ungefähr so aussieht:
400 0
396 0
392 1
400 0
396 1
400 1
das klassieren schaffe ich mit
sigma(:,1)=unique(Lasthorizonte(:,1));
wie schaffe ich es jetzt aber in sigma(:,2) die Anzahl der jeweiligen Werte einzutragen? Versucht habe ich es mit
sigma(:,2)=sum(Lasthorizonte==1,2);
da erhalte ich allerdings die Fehlermeldung "Unable to perform assignment because the size of the left side is 3-by-1 and the size of the right side is 12-by-1.".
Ziel wäre es, dass ich in sigma (:,2) die Anzahl der "1" und in sigma(:,3) die Anzahl der "0" je Zahl in der ersten Spalte erhalte.

Risposta accettata

Walter Roberson
Walter Roberson il 3 Apr 2021
Lasthorizonte = [
400 0
396 0
392 1
400 0
396 1
400 1];
[sigma, ~, uidx] = unique(Lasthorizonte(:,1));
counts = accumarray([uidx, 2-Lasthorizonte(:,2)], 1);
sigma = [sigma, counts];
sigma
sigma = 3×3
392 1 0 396 1 1 400 1 2

Più risposte (1)

Dongyue
Dongyue il 22 Nov 2022
LoadHorizons = [400 0;
396 0;
392 1;
400 0;
396 1;
400 1];
tbl = tabulate(categorical(LoadHorizons(:,1)));
sigma = cell2table(tbl,'VariableNames', ...
{'Value','Count','Percent'})
sigma = 3×3 table
Value Count Percent _______ _____ _______ {'392'} 1 16.667 {'396'} 2 33.333 {'400'} 3 50

Tag

Community Treasure Hunt

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

Start Hunting!