How can I get matlab to go through the list "time" and give all the indexes that are between my upper and lower range for "times"
조회 수: 6 (최근 30일)
이전 댓글 표시
This is my code so far. Essentially, I need to find plus and minus 5 seconds from each data point in array_times_L, and then find the corresponding indices in the array time so that I can plot the signal at each of those indices. This is my code so far, please let me know if there's anything I need to clarify.
array_mouse_L_star = table2array(mouse_L_star)
time=array_mouse_L_star(:,2)
signal=array_mouse_L_star(:,1)
array_times_L = table2array(L_times)
%times when mouse took a bite
times = array_times_L;
lowrange=times-5;
upperrange=times+5;
%find index for times ranges when mouse took a bite
[time_idx,times_idx] = find(time(:) > lowrange & time(:) < upperrange)
signal = array_mouse_r_stick(time_idx)
%split signals into columns for each time point
allsignals = reshape(signal, 100, [])
%average across each row
avg = mean(allsignals,2)
%create x values from minus 5 to plus 5 seconds
x=(linspace(-5,5,100))'
plot(x,avg,"b-")
xlabel("Time(s)")
ylabel("Signal")
my main issue is with line 10 because it keeps saying the array do not match dimensions
채택된 답변
Voss
2022년 6월 20일
편집: Voss
2022년 6월 20일
Try this:
[time_idx,times_idx] = find(time > lowrange.' & time < upperrange.')
%
% ^^^^ column vector, i.e., one value per row
% ^^^^^^^^^^ row vector, i.e., one value per column
%
%^^^^^^^^ row index
% ^^^^^^^^^ column index
time_idx will be the indices of the rows where the condition given to find is true; times_idx will be the columns.
Therefore, the matrix given to find must have rows that correspond to time and columns that correspond to times.
As it is now, time and times (and therefore lowrange and upperrange) are column vectors (looks like), so using the transpose of lowrange and upperrange makes the expression
time > lowrange.' & time < upperrange.'
a matrix whose rows correspond to elements of time and whose columns correspond to elements of times (and therefore lowrange and upperrange).
(Note that the situation is different from your previous question, where everything was row vectors, so only time needed to be converted into a column vector, which was done using time(:).)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!