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

40 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Abr. de 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

Más respuestas (1)

Dongyue
Dongyue el 22 de Nov. de 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

Etiquetas

Community Treasure Hunt

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

Start Hunting!