Sorting Column Variable to a Row
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Huw Wadkin
am 16 Jun. 2021
Kommentiert: Stephen23
am 16 Jun. 2021
I have been given my data in 3 Columns of a table as depicted in the below screenshot (I have had to try and replicate a similar scenario using excel as I can't share the actual data):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/654910/image.png)
and I wish to have it in this format for easier analysis/plots such as surface plots, with NaN is cells where there is no value for that date and category:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/654915/image.png)
Any help would be appreciated.
Thanks,
Huw
0 Kommentare
Akzeptierte Antwort
Stephen23
am 16 Jun. 2021
Bearbeitet: Stephen23
am 16 Jun. 2021
Do NOT use loops for this, the inbuilt tools are much better! First lets create some fake data in a table:
C = randi(6,30,1);
D = datetime(2021,1,randi(3,30,1));
T = unique(table(D,C),'rows');
T.V = randi([10,100],height(T),1)/10
U = unstack(T,'V','C', 'VariableNamingRule','preserve')
3 Kommentare
Stephen23
am 16 Jun. 2021
@Huw Wadkin: what options you have available depends on what MATLAB version you are using, which so far you have not told us. Use the local help of your installed MATLAB to know what options you can use.
"What do these options do?"
Weitere Antworten (1)
SALAH ALRABEEI
am 16 Jun. 2021
Bearbeitet: SALAH ALRABEEI
am 16 Jun. 2021
Assume your table ( without labels) is of size nx3; where the 1st col is your dates (in numbers), 2nd is your cat, and the 3rd is the values. See this example
x=[ 1 1 1 2 2 2 2 3 3];
y=[0:8];
z=round(100*rand(1,9));
D = [x;y;z]';
a=unique(D(:,1));
B=nan*ones(length(a),size(D,1));
for i = 1:length(a)
B(i,a(i)==D(:,1))=D(a(i)==D(:,1),3);
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Categorical Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!