Is it possible to give an categorical date set values so it can be used for a plot.

5 Ansichten (letzte 30 Tage)
I have a variable in my workspace: name=Wind_dir value=2560x1 categorical
These are wind directions (N, NNW, NW, NWW, ect) i can not figure out how to edit this. Is it possible to give like a value to N so i can use it for a plot or do i first need to de-categorical it or something like that. I dont have an idea where to begin.

Akzeptierte Antwort

Dave B
Dave B am 19 Nov. 2021
Bearbeitet: Dave B am 19 Nov. 2021
Note that you can plot categorical variables in a variety of ways. Passing your categoricals directly into your plotting function can be really useful!
c = categorical(["Apple" "Banana" "Orange"]);
y = [1 2 3];
nexttile;bar(c,y)
c = categorical(["Apple" "Banana" "Banana" "Banana" "Apple" "Orange" ...
"Apple" "Orange" "Banana" "Banana" "Orange"]);
nexttile;histogram(c)
nexttile;pie(c)
nexttile;plot(c)
You can also convert categoricals to numeric...if you want. Calling double on a categorical will convert the first category to 1, the second category to 2, etc. Note that the order of categories typically depends on the second argument to categorical, or you can change it after the fact if you use reordercats. If you want to know the map between the categories and the numbers, you can use categories:
c = categorical(["Apple" "Orange" "Banana" "Banana"])
c = 1×4 categorical array
Apple Orange Banana Banana
double(c)
ans = 1×4
1 3 2 2
categories(c)
You may also want to map some specific values for categoricals. You could define an equation to map them (in the case where they're ordered such that you can do some math from values like above:
c = categorical(["North" "North" "South" "South" "West" "West" ...
"South" "East" "North" "North"], ["North" "East" "South" "West"])
c = 1×10 categorical array
North North South South West West South East North North
(double(c)-1)*90
ans = 1×10
0 0 180 180 270 270 180 90 0 0
But in the more general case, you can look them up by defining a table (or two variables) that relates the categoricals to the numerics you want associated with them. Here's a 'fun' way to do that without a loop:
cardinal=categorical(["North";"East";"South";"West"]);
ang=[0;90;180;270];
t=table(cardinal,ang);
[r,~]=find(c==t.cardinal);
t.ang(r)
ans = 10×1
0 0 180 180 270 270 180 90 0 0
figure
polarhistogram(deg2rad(t.ang(r)),deg2rad(-45:90:315));
set(gca,'ThetaZeroLocation','top','ThetaDir','clockwise')
  3 Kommentare
Peter Perkins
Peter Perkins am 23 Nov. 2021
It's not widely known that, if c is a categorical, you can index into another array whose order is the same as c's categories. So IIUC, this step
[r,~]=find(c==t.cardinal);
in Dave's code would not be necessary. This
t.ang(c)
would do the trick.
Dave B
Dave B am 23 Nov. 2021
Unfortunately, it appears this trick doesn't work with tables, but you can use Peter's indexing magic with vectors. Though it's worth noting that if you do that you better make sure that they both specify the same order (which I skipped in my creation of cardinal above
c = categorical(["North" "North" "South" "South" "West" "West" ...
"South" "East" "North" "North"], ["North" "East" "South" "West"]);
cardinal=categorical(["North";"East";"South";"West"], ["North";"East";"South";"West"]);
ang=[0;90;180;270];
ang(c)
ans = 10×1
0 0 180 180 270 270 180 90 0 0
% Unfortunately this isn't allowed:
t = table(cardinal, ang);
t.ang(c)
Error using tabular/dotParenReference (line 101)
A table row subscript must be a numeric array containing real positive integers, a logical array, a character vector, a string array, or a cell array of character vectors.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by