필터 지우기
필터 지우기

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

조회 수: 38 (최근 30일)
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.

채택된 답변

Walter Roberson
Walter Roberson 2021년 4월 3일
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

추가 답변 (1개)

Dongyue
Dongyue 2022년 11월 22일
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

태그

Community Treasure Hunt

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

Start Hunting!