# Computing findpeaks along 3-Dimensional matrix

8 views (last 30 days)
Vivek on 19 Oct 2020
Commented: Star Strider on 20 Oct 2020
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?

Star Strider on 20 Oct 2020
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.

Star Strider on 20 Oct 2020
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.
Vivek on 20 Oct 2020
Yep, this has done it. Thank you very much!
Star Strider on 20 Oct 2020
My pleasure!
.