how do i extract 3 separate groups from one column

How do i extract 3 groups from one coloumn in association to their mean e.g:
group mean
1 24
1 34
2 60
2 12
3 14
3 12
How do i extract the data from each group separately and plot

 採用された回答

Image Analyst
Image Analyst 2022 年 4 月 15 日

0 投票

Try this:
groupMean = [...
1 24
1 34
2 60
2 12
3 14
3 12];
uniqueGroups = unique(groupMean(:, 1))
uniqueGroups = 3×1
1 2 3
for g = 1 : length(uniqueGroups)
thisGroup = uniqueGroups(g);
thisGroupsRows = groupMean(:, 1) == thisGroup;
theseMeans = groupMean(thisGroupsRows, 2); % Get value in column 2
% bar(theseMeans);
plot(theseMeans, '.-', 'LineWidth', 2, 'MarkerSize', 40)
hold on;
legendStrings{g} = sprintf('Group %d', thisGroup);
end
grid on;
legend(legendStrings)

6 件のコメント

Savannah Britto
Savannah Britto 2022 年 4 月 15 日
THjank you. i ran the data but there is an error message of this line: theseMeans = groupMean(thisGroupsRows, 2); % Get value in column 2
Image Analyst
Image Analyst 2022 年 4 月 15 日
Show your code, because as you can see I literally ran it in the text box above and it ran fine. Maybe you don't have a matrix. Maybe you have a table - I don't know because you forgot to attach the actual data. So I don't know the exact type of variable(s) they are. Attach your matrix if you still have trouble.
Savannah Britto
Savannah Britto 2022 年 4 月 15 日
編集済み: Savannah Britto 2022 年 4 月 16 日
Image Analyst
Image Analyst 2022 年 4 月 16 日
@Savannah Britto I'm not sure how you adapted my code since you forgot to attach it. However it seems to work fine for me with no error:
% subj group trial logmIKI
data = readmatrix('advancedstats2.csv');
groupNumbers = data(:, 2);
logmIKI = data(:, 4);
uniqueGroups = unique(groupNumbers)
for g = 1 : length(uniqueGroups)
% Get the group we'll use on this iteration.
thisGroup = uniqueGroups(g);
% Find out what rows this group number appears in.
thisGroupsRows = groupNumbers == thisGroup;
% Get logmIKI value.
theseMeans = logmIKI(thisGroupsRows);
% bar(theseMeans);
plot(theseMeans, '-', 'LineWidth', 2)
hold on;
legendStrings{g} = sprintf('Group %d', thisGroup);
end
grid on;
legend(legendStrings)
Of course each group has different number of elements, and since I'm plotting the x value as just the index, the plots have different widths in the x direction.
Savannah Britto
Savannah Britto 2022 年 4 月 16 日
thank you!
Image Analyst
Image Analyst 2022 年 4 月 16 日
You didn't click "Accept this answer" so I'm wondering what's left undone? Did my code not do what you expected?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by