# calculating the variability between three different mice and within one mouse code. pleas help to confirm works as intended

2 Ansichten (letzte 30 Tage)
kevin am 30 Apr. 2023
Kommentiert: Star Strider am 1 Mai 2023
Hi all, I have 6 mat files of data corresponding to 3 mice where 2 of the mat files correspond to 1 mouse yielding two mat files for each mouse.
I want to determine the variability within one mouse and then the variability between the mice, i have made a code to do this but as a novice matlab user i want to confirm and improve it. Can someone please help me to do this? Thanks!
% read in data from mat files
data = [];
for i = 1:3
mouse_data = [];
for j = 1:2
filename = sprintf('feature%d.mat', (i-1)*2+j);
mouse_data = [mouse_data temp.(sprintf('feature%d', (i-1)*2+j))];
end
data = [data mouse_data];
end
% calculate the mean and variance for each mouse
mouse_mean = mean(data, 2);
mouse_var = var(data, 0, 2);
% calculate the mean and variance for all mice together
group_mean = mean(data(:));
group_var = var(data(:));
% plot the variance within and between the mice
figure;
bar([1 2 3], mouse_var);
xlabel('Mouse');
ylabel('Variance');
title('Variance within each mouse');
ylim([0 0.3]);
figure;
bar([1 2], [group_var, mean(mouse_var)]);
xlabel('Group');
ylabel('Variance');
title('Variance between mice and within-group variance');
ylim([0 0.3]);
xticklabels({'Between mice', 'Within-group'});
##### 4 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Chanille am 30 Apr. 2023
@star strider
Star Strider am 1 Mai 2023
See the documentation I linked to, first to be certain that it does what you want (since I’m not certain that I sufficiently understand the problem to be solved), and second to understand how to use the function and interpret its results.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

LeoAiE am 30 Apr. 2023
Your code reads the data from the .mat files, concatenates them, and calculates the mean and variance for each mouse and the group. However, there are a few improvements and adjustments that you could make to better represent the variability within and between mice.
1. Calculate the mean and variance for each mouse separately, without concatenating the data.
2. Calculate the mean and variance for each individual trial and then calculate the overall mean and variance for each mouse.
Here's an updated version of your code that takes these points into consideration:
% Read in data from mat files
data = cell(3,2);
for i = 1:3
for j = 1:2
filename = sprintf('feature%d.mat', (i-1)*2+j);
data{i, j} = temp.(sprintf('feature%d', (i-1)*2+j));
end
end
% Calculate the mean and variance for each mouse and trial
mouse_mean = zeros(3, 1);
mouse_var = zeros(3, 1);
trial_mean = cell(3, 2);
trial_var = cell(3, 2);
for i = 1:3
trial_data = [];
for j = 1:2
trial_mean{i, j} = mean(data{i, j}, 2);
trial_var{i, j} = var(data{i, j}, 0, 2);
trial_data = [trial_data data{i, j}];
end
mouse_mean(i) = mean(trial_data(:));
mouse_var(i) = var(trial_data(:));
end
% Calculate the mean and variance for all mice together
group_mean = mean(cell2mat(data(:)));
group_var = var(cell2mat(data(:)));
% Plot the variance within and between the mice
figure;
bar([1 2 3], mouse_var);
xlabel('Mouse');
ylabel('Variance');
title('Variance within each mouse');
ylim([0 0.3]);
figure;
bar([1 2], [group_var, mean(mouse_var)]);
xlabel('Group');
ylabel('Variance');
title('Variance between mice and within-group variance');
ylim([0 0.3]);
xticklabels({'Between mice', 'Within-group'});
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Annotations finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by