How to make a bar graph out of the first and second column of a matrix?

5 views (last 30 days)
I have matrix
sleeprxn2= [ 0 0.6229
1.0000 0.3404
1.0000 0.2744 ]
I would like if sleeprxn2 is equal to 0, then plot on bar graph 0.6299, and if sleeprxn2 is equal to 1, then plot the mean of the numbers (0.3404 and 0.2744). I would like this to be applied to many more numbers in the future.. I've tried what you see below.
I feel like it shouldn't be this difficult.
rRbar = [0.6229 0.3404 0.2744]
sleep_vec = [0,1,1]
% plot of reaction times to hours sleep
figure
subplot(1,2,1)
bar(sleep_vec,rRbar,'r')
hold on;
subplot(1,2,2)
bar(sleep_vec,rBbar,'b')
% needs worked
%%%%%%
morethan=0
lessthan=0
more8=[];
less8=[];
for i=1:length(sleep_vec)
if sleep_vec >= 1
morethan=morethan+1
else
sleep_vec < 1
lessthan=lessthan+1
end
more8=[more8 morethan];
less8=[less8 lessthan];
end
subplot(1,2,1);
bar(more8,rR,'r');
hold on
subplot(1,2,2);
bar(less8,rB,'b');
%%%%%
for i=1:length(sleep_vec)
if sleep_vec == 1
plot(sleep_vec,'ro')
else
sleep_vec == 0
plot(sleep_vec,'bo')
end
end
%%%%%%red reaction time to sleep
sleeprxn=[sleep_vec rRbar]
sleeprxn2=reshape(sleeprxn,3,[])
for i=1:length(sleep_vec)
if sleeprxn2 == 1
plot(sleeprxn2(:,2));
else sleeprxn2 == 0
plot(sleeprxn2(:,2));
end
end
s=sleeprxn2
x=s(:,1);
y=s(:,2);
bar(x,y)
greater=numel(find(sleep_vec==1));
bar(greater,rRbar,'r');
hold on
less=numel(find(sleep_vec==0));
bar(less,rRbar,'g');
end
sleeprxn=[sleep_vec rRbar]
sleeprxn2=reshape(sleeprxn,3,[])
rR=reshape(reactionTime_red_vec,2,[])

Accepted Answer

Adam Danz
Adam Danz on 24 Nov 2021
sleeprxn2= [ 0 0.6229
1.0000 0.3404
1.0000 0.2744 ]
sleeprxn2 = 3×2
0 0.6229 1.0000 0.3404 1.0000 0.2744
[groups, groupID] = findgroups(sleeprxn2(:,1));
groupMeans = splitapply(@mean, sleeprxn2(:,2), groups);
bar(groupID, groupMeans)

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!