Computing findpeaks along 3-Dimensional matrix

I have a 3-dimensional matrix, after some code manipulation on a cell array. The matrix is of size 101x151x476.
The first number represents the y space, the second number is the x space, and the third number is the number of time steps. So essentially, the matrix contains magnitudes along a y-x plane, for varying timesteps.
I am trying to compute the peaks at each (y,x) across the timesteps. I am doing this using findpeaks. This is what I have so far:
for yIndex= 1:1:101
for xIndex= 1:1:151
[peaks{y,x}] = findpeaks(a3Dmatrix(yIndex,xIndex,:));
end
end
However, the cell "peaks" is incorrect, as it is not of the size 101x151 (y by x). Any help with this?

 采纳的回答

I am guessing here, since I do not have your data. Since findpeaks is going to return vectors for the peaks, locations (and othher outputs if you want them), I would do something like this:
for yIndex= 1:1:101
for xIndex= 1:1:151
[peaks{yIndex,xIndex,:},locs{yIndex,xIndex,:}] = findpeaks(a3Dmatrix(yIndex,xIndex,:));
end
end
I cannot test that because I do not have your data, so I am posting it as UNTESTED CODE.

6 个评论

Thanks. I've given this a try, but there is an error message.
"Expected one output from a curly brace or dot indexing expression, but there were 0 results."
I do not understand what line threw that error. Cell arrays can certainly contain empty vectors (denoted as {[]}), and it is certainly possible that there are no identifiable peaks in your signal (constant, or that the maximum is at one or the other end of the vector, so not a ‘peak’ as findpeaks defines it). That would result in findpeaks returning an empty output for that particular set of coordinates.
Perhaps a better option is:
for yIndex= 1:1:101
for xIndex= 1:1:151
[pks,lcs] = findpeaks(a3Dmatrix(yIndex,xIndex,:));
peaks{yIndex,xIndex} = pks;
locs{yIndex,xIndex} = lcs;
end
end
That might make it easier to understand what the problem is, since you could look at the size function output of size(pks).
If that indicates an empty vector, then:
[max,idx] = maxk(a3Dmatrix(yIndex,xIndex,:), 2);
could suggest what the problem is.
You could put that in an if block to trap the empty vector, for example:
for yIndex= 1:1:101
for xIndex= 1:1:151
[pks,lcs] = findpeaks(a3Dmatrix(yIndex,xIndex,:));
if (numel(pks) < 1))
[pks,lcs] = maxk(a3Dmatrix(yIndex,xIndex,:), 2);
end
peaks{yIndex,xIndex} = pks;
locs{yIndex,xIndex} = lcs;
end
end
Then later look at ‘locs’. If they are the first or last 2 points in the vector (or the first and last points), there are no peaks in that segment.
Thanks for the reply. I've actually written up my version of the code again, but instead of findpeaks, i'm trying "mean" and also "max", and the output is the correct 101x151 size. However, with findpeaks, the error message is "Expected Y to be a vector".
So it seems that MATLAB's findpeaks function requires some more code before being able to tackle a 3-dimensional matrix. That's where I am stuck, cause I don't know how to adapt my code to get it to go through each (y,x) along the time dimension, in order to produce the peaks. Also, do note that I have checked each (y,x) along the time dimension, by plotting time series plots, and there are clean peaks at every (y,x) along the time dimension. So I know that my matrix has good peaks. Can you please look at my code and suggest what can be edited to get it to produce find these peaks?
peaks = cell(dy_cap,dx_cap);
for y=1:1:101
for x=1:1:151
peaks{y,x} = findpeaks(a3Dmatrix(y,x,:));
end
end
The problem is that I did not test my code.
Try this version:
for yIndex= 1:size(a3Dmatrix,1)
for xIndex= 1:size(a3Dmatrix,2)
vect = squeeze(a3Dmatrix(yIndex,xIndex,:));
[pks,lcs] = findpeaks(vect);
if (numel(pks) < 1)
[pks,lcs] = maxk(vect, 2);
end
peaks{yIndex,xIndex} = pks;
locs{yIndex,xIndex} = lcs;
end
end
It ran without error with a random 3D matrix, and appeared to produce reasonable results.
Yep, this has done it. Thank you very much!
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by