How can I transpose 2 matrices to show 2 experimental conditions?

1 次查看(过去 30 天)
Hello,
I'm trying to plot reaction times while masked vs. unmasked. I have 2 variables, the averages of reaction times for 17 subjects in a 1x17 array, labelled mean_RT_m, and new_mean_RT
I was able to produce the accompanying image with the following code
figure;
plot( new_mean_RT, 'o');
hold on;
plot(mean_RT_m, 'o', 'LineWidth', 4);
xlabel('Subject number'), ylabel('Reaction time (ms)'), title('Reaction times of participants')
legend('unmasked','masked')
ax = gca;
ax.XLim = [0.5 17.5];
ax.XTick = [3 6 9 12 15];
set(gca,'FontSize',16);
However, given that it's a bit difficult to read, I'm trying to separate it by condition instead of subject number. So I transposed the matrices thinking it would give me 2 large arrays as so
A = new_mean_RT.'
B = mean_RT_m.'
%substituted A and B into figure plot areas
However, it simply gave me the same image. I know Ill need to change the legend and labels to reflect the update, but what am I missing to make it into effectively 2 separate variables?

采纳的回答

Dave B
Dave B 2021-10-1
编辑:Dave B 2021-10-1
You need to give MATLAB some information about where these values should go on the x axis. Here's your orignal plot and then a couple of strategies - the first uses plot with a numeric axis (the values are associated with the numbers 1 and 2), the second swaps in scatter (which is a better tool if you're making a scatter-like plot), and the third uses scatter with a categorical axis. (In the last case I also included some code to set the legend names directly when you plot):
new_mean_RT = rand(1,17);
mean_RT_m = rand(1,17);
plot( new_mean_RT, 'o');
hold on;
plot(mean_RT_m, 'o', 'LineWidth', 4);
xlabel('Subject number'), ylabel('Reaction time (ms)'), title('Reaction times of participants')
legend('unmasked','masked')
ax = gca;
ax.XLim = [0.5 17.5];
ax.XTick = [3 6 9 12 15];
set(gca,'FontSize',16);
clf
plot(1, new_mean_RT, 'ro')
hold on
plot(2, mean_RT_m, 'bo')
xlim([0 3])
xticks([1 2])
xticklabels(["masked" "unmasked"])
clf
scatter(ones(size(new_mean_RT)), new_mean_RT);
hold on
scatter(2*ones(size(mean_RT_m)), mean_RT_m);
xlim([0 3])
xticks([1 2])
xticklabels(["masked" "unmasked"])
clf
cat1=repelem(categorical("Masked"), numel(new_mean_RT));
cat2=repelem(categorical("Unmasked"), numel(mean_RT_m));
scatter(cat1, new_mean_RT, 'DisplayName', 'Masked')
hold on
scatter(cat2, mean_RT_m, 'DisplayName', 'Unmasked')
legend
  2 个评论
Ruth Ronalter
Ruth Ronalter 2021-10-4
Thanks for the help, now I just need to figure out how to plot the mean of the conditions conditions. I tried the mean function for the masked one, and it came out NaN, and the Unmasked condition entries now have circles around them.
Dave B
Dave B 2021-10-8
编辑:Dave B 2021-10-8
If you have a NaN in the mean for the masked condition that almost certainly means that you have a NaN in one of the values for the masked condition. Do you know where that NaN came from? Should that data point be ignored in the mean? If so, adding the omitnan flag is an easy way to ignore it:
mean([1 2 nan 3 4],'omitnan')
ans = 2.5000
Not sure what you meant about the circles, but I'm sure we can debug it if you show some more detail (here or in a new question).
Here's a demo with mean lines (I used the middle version of the above). I took advantage of the 'SeriesIndex' property here which makes the lines match the colors of the corresponding scatters (assuming they're autocolored by MATLAB). You might also consider using bars underneath the scatters instead of lines above them? Or a boxchart? Limitless options!
new_mean_RT = rand(1,17);
mean_RT_m = rand(1,17);
scatter(ones(size(new_mean_RT)), new_mean_RT);
hold on
scatter(2*ones(size(mean_RT_m)), mean_RT_m);
x = [.8 1.2];
y = repelem(mean(new_mean_RT),2);
plot(x,y,'LineWidth',2,'SeriesIndex',1)
x = [1.8 2.2];
y = repelem(mean(mean_RT_m),2);
plot(x,y,'LineWidth',2,'SeriesIndex',2);
xlim([0 3])
xticks([1 2])
xticklabels(["masked" "unmasked"])

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by