我有一个函数plot_result()
,它根据测试从结果中绘制条形图。我有两个向量:
R = [0 1 1 -1] containing logical true or false from the questions. and
T = [0 1 2 3] containing time spent on each questions.
0 - in R means a question not answered
如果答案为1,则栏为绿色,否则为红色。 但传说显示的不是预期。
在上述情况下R和T 传奇节目:
Green - "Right answer"
Green - "Right answer"
而不是
Green - "Right answer"
Red - "Wrong answer"
有时它也显示3个传说而不是2个 - 我做错了什么?
R = [0 1 1 -1];
T = [0 1 2 3];
if sum(T) ~= 0
% Barchart
figure;
for i=1:length(T)
h(i) = bar(i, T(i));
if i == 1 hold on, end
if R(i) == -1
col = 'r';
legendInfo{i} = ['Wrong Answer '];
else
col = 'g';
legendInfo{i} = ['Right Answer '];
end
set(h(i), 'FaceColor', col)
end
end
set(gca,'xTick',1:length(R),'xTickLabel',1:length(R))
xlabel('Answer No..');
ylabel('Time per. question')
title('Question vs. time')
set(gca,'YLim',[0 max(T)])
[~,legendsIWant]=unique(nonzeros(R));
legend(h(legendsIWant),legendInfo(legendsIWant),'Location', 'BestOutside' 'FontSize', 8);
答案 0 :(得分:1)
给定输入的问题是[~,legendsIWant]=unique(nonzeros(R))
将给出与非零(R)对应的索引值而不是R
可能的解决方法是获取所有唯一值的位置,然后丢弃任何对应的零 例如替换
中提到的行[uniqueValues,legendsIWant]=unique(R)
legendsIWant=legendsIWant(uniqueValues~=0)