MATLAB - Working with accumarray with multiple categories? - matlab

I am asking a follow-up my question here, in which there was a perfect solution that did exactly what I wanted. But I'm wondering how to apply this method, or do something similar, if instead of yes/no as possible responses, I would have more than 2 responses, so yes/no/maybe, for example. Or how it would generalize to 3+ responses.
This is the answer, reformatted as my question:
Assuming my data looks like this:
responses = categorical(randi(3,1250,1),[1 2 3],{'no','yes','maybe'});
race = categorical(randi(5,1250,1),1:5,{'Asian','Black','BHispanic','White','WHispanic'});
I would like to go through and do the same thing with my yes/no data, but do this with 3 possibilities, or more. And this will not end up working anymore:
% convert everything to numeric:
yn = double(responses);
rac = double(race);
% caluculate all frequencies:
data = accumarray(rac,yn-1);
data(:,2) = accumarray(rac,1)-data;
% get the categories names:
races = categories(race);
answers = categories(responses);
% plotting:
bar(data,0.4,'stacked');
ax = gca;
ax.XTickLabel = races; % set the x-axis ticks to the race names
legend(answers) % add a legend for the colors
colormap(lines(3)) % use nicer colors (close to your example)
ylabel('YES/NO/MAYBE')% set the y-axis label
% some other minor fixes:
box off
ax.YGrid = 'on';
I'm not sure if there is even a way to use the accumarray method to do this, as it doesn't make sense from my understanding to use this with 3 possible responses. I'd like to generalize it to n possible responses too.
UPDATE: I'm currently investigating the crosstab feature which I didn't find at all until now! I think this may be the feature I'm looking for.

Here is a generalized version:
% the data (with even more categories):
yesno = categorical(randi(4,1250,1),1:4,{'no','yes','maybe','don''t know'});
race = categorical(randi(5,1250,1),1:5,{'Asian','Black','BHispanic','White','WHispanic'});
% convert everything to numeric:
yn = double(yesno);
rac = double(race);
% caluculate all frequencies:
data = accumarray([rac yn],1);
% get the categories names:
races = categories(race);
answers = categories(yesno);
% plotting:
bar(data,0.4,'stacked');
ax = gca;
ax.XTickLabel = races; % set the x-axis ticks to the race names
legend(answers) % add a legend for the colors
colormap(lines(numel(answers))) % use pretier colors
ylabel('YES/NO')% set the y-axis lable
% some other minor fixes:
box off
ax.YGrid = 'on';
The result:
And in a table:
T = array2table(data.','VariableNames',races,'RowNames',answers)
the output:
T =
Asian Black BHispanic White WHispanic
_____ _____ _________ _____ _________
no 58 72 69 66 62
yes 58 53 72 54 58
maybe 63 62 67 62 61
don't know 58 57 66 58 74
As you already mentioned, you can use crosstab for the same task. crosstab(rac,yn) will give you the same result as accumarray([rac yn],1). I think accumarray is faster, though I didn't check it.

Related

Calculate integral in Matlab [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am given the following data:
and I am asked to calculate the integral of Cp/T dT from 113.7 to 264.4.
I am unsure of how I should solve this. If I want to use the integral command, I need a function, but I don't know how my function should be in that case.
I have tried:
function func = Cp./T
T = [...]
Cp=[...]
end
but that didn't work.
Use the cumtrapz function in MATLAB.
T = [...]
Cp=[...]
CpdivT = Cp./T
I = cumtrapz(T, CpdivT)
You can read more about the function at https://www.mathworks.com/help/matlab/ref/cumtrapz.html
A simple approach using interp1 and integral using plain vanilla settings.
Would only use more sophisticated numerical techniques if required for application. You can examine the 'RelTol' and 'AbsTol' options in the documentation for integral.
Numerical Integration: (w/ linear interpolation)
% MATLAB R2017a
T = [15 20 30 40 50 70 90 110 130 140 160 180 200 220 240 260 270 275 285 298]';
Cp = [5.32 10.54 21.05 30.75 37.15 49.04 59.91 70.04 101.59 103.05 106.78 ...
110.88 114.35 118.70 124.31 129.70 88.56 90.07 93.05 96.82]';
fh =#(t) interp1(T,Cp./T,t,'linear');
t1 = 113.7;
t2 = 264.4;
integral(fh,t1,t2)
ans = 91.9954
Alternate Methods of Interpolation:
Your results will depend on your method of interpolation (see code & graphic below).
% Results depend on method of interpolation
Linear = integral(#(t) interp1(T,Cp./T,tTgts,'linear'),t1,t2) % = 91.9954
Spline = integral(#(t) interp1(T,Cp./T,tTgts,'spline'),t1,t2) % = 92.5332
Cubic = integral(#(t) interp1(T,Cp./T,tTgts,'pchip'),t1,t2) % = 92.0383
Code for graphic:
tTgts = T(1):.01:T(end);
figure, hold on, box on
p(1) = plot(tTgts,interp1(T,Cp./T,tTgts,'linear'),'b-','DisplayName','Linear')
p(2) = plot(tTgts,interp1(T,Cp./T,tTgts,'spline'),'r-','DisplayName','Spline')
p(3) = plot(tTgts,interp1(T,Cp./T,tTgts,'pchip'),'g-','DisplayName','Cubic')
p(4) = plot(T,Cp./T,'ks','DisplayName','Data')
xlim([floor(t1) ceil(t2)])
legend('show')
% Cosmetics
xlabel('T')
ylabel('Cp/T')
for k = 1:4, p(k).LineWidth = 2; end
A poor approximation: (to get rough order of magnitude estimate)
tspace = T(2:end)-T(1:end-1);
midpt = mean([Cp(1:end-1) Cp(2:end)],2);
sum(midpt.*tspace)./sum(tspace)
And you can see we're in the ballpark (makes me feel more comfortable at least).
Other viable MATLAB Functions: quadgk | quad
% interpolation method affects answer if using `interp1()`
quadgk(#(t) interp1(T,Cp./T,t,'linear'),t1,t2)
quad(#(t) interp1(T,Cp./T,t,'linear'),t1,t2)
Functions that would require more work: trapz | cumtrapz
Notice that trapz and cumtrapz both require unit spacing; to use these would require first interpolating with unit spacing.
Related Posts: (found after answer already completed)
Matlab numerical integration
How to numerically integrate vector data in Matlab?
This is probably better for your problem. Take note that I have assumed 2nd order polynomial fits your data well. You may want to get a better model structure if the fit is unsatisfactory.
% Data
T = [15 20 30 40 50 70 90 110 130 140 160 180 200 220 240 260 270 275 285 298];
Cp = [5.32 10.54 21.05 30.75 37.15 49.04 59.91 70.04 101.59 103.05 106.78 110.88 114.35 118.70 124.31 129.70 88.56 90.07 93.05 96.82];
% Fit function using 2nd order polynomial
f = fit(T',Cp'./T','poly2');
% Compare fit to actual data
plot(f,T,Cp./T)
% Create symbolic function
syms x
func = f.p1*x*x + f.p2*x + f.p3;
% Integrate function
I = int(func,113.7,264.4);
% Convert solution from symbolic to numeric value
V = double(I);
The result is 92.7839

Multiple bar charts in one graph in Octave

Using Octave 4.2.1 on Windows with the qt graphics toolkit (I can't use gnuplot because it crashes in some other part of the code). I have a dataset which is 35x7x4 (35 data points for 7 conditions on 4 channels) - you can use random data for the purpose of this exercise.
I am trying to create 4 subplots (1 for each channel), with 7 bar graphs on each subplot (one per condition) to see how the distribution of data changes with each condition. Each of the 7x4 = 28 distributions has its own set of bins and frequencies, and I can't seem to be able to combine the 7 datasets on one graph (subplot).
Posting the whole of the code would be too complicated, but here's a simplified version:
nb_channels = 4;
nb_conditions = 7;
nbins = 15;
freq = zeros(nbins,nb_conditions,nb_channels);
xbin = zeros(nbins,nb_conditions,nb_channels);
plot_colours = [91 237 165 255 68 112 255;
155 125 165 192 114 173 0;
213 49 165 0 196 71 255];
plot_colours = plot_colours / 255;
for k = 1:nb_channels
for n = 1:nb_conditions
% some complex calculations to generate temp variable
[freq(:,n,k),xbin(:,n,k)] = hist(temp,nbins);
end
end
figure
for k = 1:nb_channels
subplot(2,2,k)
for n = 1:nb_conditions
bar(xbin(:,n,k),freq(:,n,k),'FaceColor',plot_colours(:,n))
hold on
end
hold off
legend('condition #1','condition #2','condition #3','condition #4','condition #5','condition #6','condition #7')
end
which gives something like this:
So you can't really see anything, all the bars are on top of each other. In addition, Octave doesn't support transparency property for patch objects (which is what bar charts use), so I can't overlay the histograms on top of each other, which I would really quite like to do.
Is there a better way to approach this? It seems that bar will only accept a vector for x data and not a matrix, so I am stuck in having to use hold on and loop through the various conditions, instead of using a matrix approach.
OK, so I'll try to answer my own question based on the suggestions made in the comments:
Suggestion 1: make all the bins the same
This does improve the results somewhat but it's still an issue due to the lack of transparency for patch objects.
Code changes:
nbins = 15;
xbin = linspace(5.8,6.5,nbins);
for k = 1:nb_channels
for n = 1:nb_conditions
% some complex calculations to generate temp variable
freq_flow(:,n,k) = hist(temp,xbin);
end
end
figure
for k = 1:nb_channels
subplot(2,2,k)
for n = 1:nb_conditions
bar(xbin,freq_flow(:,n,k),'FaceColor',plot_colours(:,n))
hold on
end
hold off
xlim([5.8 6.3])
legend('condition #1','condition #2','condition #3','condition #4','condition #5','condition #6','condition #7')
end
Which gives the following plot:
Suggestion 2: Use line plots instead of bar charts
This helps a bit more in terms of readability. However, the result is a bit "piece-wise".
Code changes:
figure
for k = 1:nb_channels
subplot(2,2,k)
for n = 1:nb_conditions
plot(xbin,freq_flow(:,n,k),'LineStyle','none','marker','.',...
'markersize',12,'MarkerEdgeColor',plot_colours(:,n),...
'MarkerFaceColor',plot_colours(:,n))
hold on
end
hold off
xlim([5.8 6.3])
legend('condition #1','condition #2','condition #3','condition #4','condition #5','condition #6','condition #7')
end
Which gives the following result:
The legend is a bit screwed but I can probably sort that out.
A variation on this I also tried was to plot just the points as markers, and then a fitted normal distribution on top. I won't post all the code here, but the result looks something like this:
Suggestion 3: transparency workaround with gnuplot
Unfortunately, before I even got to the transparency workaround, gnuplot keeps crashing when trying to plot the figure. There's something it doesn't like with subplots and legends I think (which is why I moved to qt graphics toolkit in the first place, as I had exactly the same issue in other parts of the code).
Solution 4: use 3D bar graph
I found this on SO: 3D histogram with gnuplot or octave
and used it as such:
figure
for k = 1:size(flow_factor,2)
subplot(2,2,k)
h = my_bar3(freq_flow(:,:,k));
fvcd = kron((1:numel(freq_flow(:,:,k)))', ones(6,1));
set(h, 'FaceVertexCData',fvcd, 'FaceColor','flat', 'CDataMapping','scaled')
colormap hsv; axis tight; view(50,25)
ylbl = cell(length(xbin),1);
for k=1:length(xbin)
ylb{k} = num2str(xbin(k));
end
set(gca,'YTick',1:2:nbins);
set(gca,'YTickLabel',ylb(1:2:end));
end
to produce:
Which isn't bad, but probably not as clear as the line plots.
Conclusion
On balance, I will probably end up using one of the line plots approaches, as they tend to be clearer.

how to make a continuous stacked bar graph

Does someone know how to make a graph similar to this one with matlab?
To me it seems like a continuous stacked bar plot.
I did not manage to download the same data so I used other ones.
I tried the following code:
clear all
filename = 'C:\Users\andre\Desktop\GDPpercapitaconstant2000US.xlsx';
sheet = 'Data';
xlRange = 'AP5:AP259'; %for example
A = xlsread(filename,sheet,xlRange);
A(isnan(A))=[]; %remove NaNs
%create four subsets
A1=A(1:70);
A2=A(71:150);
A3=A(151:180);
A4=A(181:end);
edges=80:200:8000; %bins of the distributions
[n_A1,xout_A1] = histc(A1,edges); %distributions of the first subset
[n_A2,xout_A2] = histc(A2,edges);
[n_A3,xout_A3] = histc(A3,edges);
[n_A4,xout_A4] = histc(A4,edges);
%make stacked bar plot
for ii=1:numel(edges)
y(ii,:) = [n_A1(ii) n_A2(ii) n_A3(ii) n_A4(ii)];
end
bar(y,'stacked', 'BarWidth', 1)
and obtained this:
It is not so bad.. Maybe with other data it would look nicer... but I was wondering if someone has better ideas. Maybe it is possible to adapt fitdist in a similar way?
First, define the x axis. If you want it to follow the rules of bar, then use:
x = 0.5:numel(edges)-0.5;
Then use area(x,y), which produces a filled/stacked area plot:
area(x,y)
And if you want the same colors as the example you posted at the top, define the colormap and call colormap as:
map = [
218 96 96
248 219 138
253 249 199
139 217 140
195 139 217
246 221 245
139 153 221]/255;
colormap(map)
(It may not be exactly as the one you posted, but I got it quite close I think. Also, not all colors are shown in the result below as there are only 4 parameters, but all colors are defined)
Result:

Carrying out edge detection on the ROI of image in Matlab

How can do edge detection on the ROI (only) of an image without processing the rest of the image? I have tried the following but it is not working:
h4 = #(x) edge(x,'log');
Edge_map = roifilt2(Foregound_Newframe,roi_mask,h4);
roi_mask is the binary mask that I am using and Foregound_Newframe is the gray image to be processed. Kindly provide an example. Thanks.
The error I see is that the function you are using to do the filtering requires input argument of type double, otherwise your calling syntax should work fine.
i.e. use
YourFilter = #(x) edge(double(x),'log');
When I apply this to an example fromroifilt2 docs it works fine (ok it looks weird in this case...):
clc
clear
FullImage = imread('eight.tif');
roi_col = [222 272 300 270 221 194];
roi_row = [21 21 75 121 121 75];
ROI = roipoly(FullImage,roi_col,roi_row);
YourFilter = #(x) edge(double(x),'log');
J = roifilt2(FullImage,ROI,YourFilter);
figure, imshow(FullImage), figure, imshow(J)
with following output:

Convert Frequency to Note in matlab

So I created a MATLAB program to detect the frequencies present in a piano recording. Now I need to convert these detected frequencies to their corresponding piano note.
I know there's a theory about using A4 (440Hz) as the reference note and deriving the rest based on that. But I'm looking for something like a look up table, where I can directly call the respective piano notes by looking at the look up table. I'm not quite sure how to go on about it though and so would like some suggestions on what I could do... Thanx
From here (just the first Google hit, nothing special), you can see that all the frequencies of all notes in the equal-tempered scale are related through
f = f0 · 2^(i/12)
with f0 = A4 = 440Hz, and i the number of half-steps away from the base note. This allows you to make a lookup table (LUT) for all the notes.
The only thing you need to put a bit of work in are:
find the entry in the LUT closest to your frequencies
map those entries to named notes
Define what the range of your source piano is (yes, ranges can differ).
Today's your lucky day (I'm bored and procrastinating :), so here's a fairly complete implementation, for notes from C0 to E8♭:
% Your frequencies (can be of any size, must be in Hz)
V = [17 450 4000];
% Generate the lookup table
LUT = 440 * (2^(1/12)).^(-57:42);
% The names of all those notes
noteNames = {...
'AN' 'AN♯/BN♭' 'BN' 'CN' 'CN♯/DN♭' 'DN' 'DN♯/EN♭' 'EN' 'FN' 'FN♯/GN♭' 'GN' 'GN♯/AN♭'};
allNotes = [];
for ii = 0:8
allNotes = [allNotes regexprep(noteNames, 'N', num2str(ii))]; end %#ok<AGROW>
allNotes = allNotes(4:end-4);
% Indices into the lookup table
[~,I] = min(abs(bsxfun(#minus, V(:), LUT)), [], 2);
% The named noted corresponding to your frequencies
allNotes(I)
You don't need a lookup table neither do you have to limit yourself to a specific range: (Javascript)
function getNoteFromFrequency(frequency) {
var noteOrder = 'G# A A# B C C# D D# E F F# G'.split(" ");
var n = Math.round(49 + 12 * Math.log(frequency / 440) / Math.log(2));
var note = noteOrder[n % noteOrder.length];
var index = Math.ceil((n - 3) / noteOrder.length);
return note + index;
}