MATLAB Answers

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

3 views (last 30 days)
Ruth Ronalter
Ruth Ronalter on 1 Oct 2021
Edited: Dave B on 8 Oct 2021
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?

Accepted Answer

Dave B
Dave B on 1 Oct 2021
Edited: Dave B on 1 Oct 2021
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 Comments
Dave B
Dave B on 8 Oct 2021
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"])

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by