Hi everyone. I need help with a moving window creation: I'm analyzing behavioral data collected from rats and I need to create a moving window showing how their psycometric curves change through each session. I don't need help with the psycometric curves creation, but I really don't know how to set the loop to create a moving window with a window of 50 and a step of 1...
I need to do this with a LOOP, so I don't want to use any function (like movmean or conv for example)
I really hope you can help me because this is driving me crazy

 채택된 답변

Image Analyst
Image Analyst 2020년 12월 15일

0 개 추천

Why don't you want to use the built in function? Is it homework?
What do you want to do for elements 1-49? Or the final 49 elements? Do you want the window to shrink as your window would leave the array? Do you just want those places where the window fits complete in your array? Did you just try a simple for loop:
ratData = rand(1, 500); % Sample data
windowWidth = 50;
averagedData = zeros(1, length(ratData) - windowWidth);
for k = 1 : length(ratData) - windowWidth
averagedData(k) = mean(ratData(k:k+windowWidth-1));
end
plot(ratData, 'b-', 'LineWidth', 2);
hold on;
plot(averagedData, 'r-', 'LineWidth', 2);
grid on;
legend('Actual', 'Averaged');
msgbox('Done!');

댓글 수: 3

Lisa Fontana
Lisa Fontana 2020년 12월 17일
편집: Lisa Fontana 2020년 12월 17일
Homework? Nono, I'm actually collecting data from rats (I'm in the lab right now).
I cannot use built function because my PI wants me to use a simple for loop because I've never done a moving window before.
For example.. I've done something similar with just 4 psycometric curves with this code:
bounds = 0:50:200;
mean_perf = nan (1, 4);
mean_Dec1 = nan (4, 9);
figure;
hold on;
cols2 = [0 0 1; 0 0.3 0.8; 0 0.4 0.7; 0 0.5 0.6];
pars2 = nan (4,4);
for iblock = 1: length(bounds)-1
block_index = data.trialID > bounds(iblock) & data.trialID <= bounds(iblock + 1) & data.waited;
mean_perf(1, iblock) = mean(data.hitmiss(block_index));
end
for iblock = 1: length (bounds)-1
block_index = data.trialID > bounds(iblock) & data.trialID <= bounds(iblock + 1) & data.waited;
for j = 1:length (stims)
meanDec1(iblock, j) = mean(data.action(stimID==stims(j) & ih1 & block_index));
count_Dec(iblock, j)= sum(stimID==stims(j) & ih1 & block_index);
end
end
for iblock = 1: length (bounds)-1
plot (stims,meanDec1(iblock,:), 'o','color',cols2(iblock,:),'markersize',4,'markerfacecolor',cols2(iblock,:));
pars2(iblock,:) = lsqcurvefit (@cumulativegaussianLapse, p0, stims, meanDec1(iblock,:));
prediction = cumulativegaussianLapse (pars2(iblock,:), xaxis);
plot (xaxis, prediction,'color',cols2(iblock,:),'linewidth',2);
end
title ('Performance trials 1-50, 51-100, 101-150, 151-200 per ih1');
xlabel ('Stims');
ylabel ('meanDec');
legend ('meanDec trials 1-50', 'Psycometric curve 1-50','meanDec trials 51-100', 'Psycometric curve 51-100','meanDec trials 101-150', 'Psycometric curve 101-150','meanDec trials 151-200', 'Psycometric curve 151-200', 'Location', 'southeast');
ylim ([0 1]);
I need to do the same thing but with many more psycometric curves, precisely with a moving window of 50 with a step of 1. I've tried many ways but I don't know why I can't figure it out, I have written much more complex code.. My idea is to set what happens in the first round of the loop, and then specify what happens from the second round on; I have the idea but I really don't know how to set the loop
Image Analyst
Image Analyst 2020년 12월 17일
Maybe he doesn't know about the movmean() function so that's the reason why he suggested a loop. I see no real NEED for it to be a loop. Suggest the built-in movmean function to him. Or just go ahead and use it. I doubt he'd really care HOW you got it done.
Anyway, didn't my code do a manual 50 element moving mean? Not sure why it didn't meet your needs.
Lisa Fontana
Lisa Fontana 2020년 12월 17일
No sorry I haven't tried your code yet, I'll do it right now.
I just wanted to explain you exactly what I ment. So do you think your code will work for what I want?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 12월 15일

0 개 추천

Use the value of the for loop variable as the index of the first element in the window. Add a quantity to get the index of the last element in the window. Use linear indexing to extract the elements in the window.
x = (1:10).^2
x = 1×10
1 4 9 16 25 36 49 64 81 100
y = x(4:7) % [16 25 36 49]
y = 1×4
16 25 36 49

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2020년 12월 15일

편집:

2020년 12월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by