How to extract peaks from a matrix and concatenate columns with different number of rows using a loop?

1 Ansicht (letzte 30 Tage)
Hi I want to extract the peaks of all columns from a matrix M.
Each column of M should generate a new column of a matrix N
with the PEAKS as elements.
I tried the following loop:
for n=1:10
NPEAKS{n} = (findpeaks(M(:,n:n)))';
ALLPEAKS=[NPEAKS{1:n}];
end;
The second line of the loop works fine
and I obtain in each { } the desired column with the peaks.
However, the concatenation (third line of the loop)
of all columns to ONE new matrix did not work
because the amount of peaks (or elements) found for each
columns differs from column to columns.
How can I correct the commands above or to obtain the same result
doing something else.
Thank you for your help
Emerson

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 20 Apr. 2011
variant of using cellfun
M = randi(1500,12,12);
[p,l] = arrayfun(@(x)findpeaks(M(:,x)),1:size(M,2),'UniformOutput',false);
iend = cellfun('size',l,2);
iendmax = max(iend);
NPEAKSdouble = cell2mat(arrayfun(@(x,y,z)[cat(3,x{:}.',y{:}.');zeros(iendmax-z,1,2)],p,l,iend,'UniformOutput',false));
variant of using for loops
M = randi(1500,12,12);
nM = size(M);
N = 0;
NPEAKSdouble = zeros([nM,2]);
for jj = 1:nM(2)
[p,l] = findpeaks(M(:,jj));
lp = length(p);
N = max(N,lp);
NPEAKSdouble(1:lp,jj,:) = cat(3,p,l);
end
NPEAKSdouble = NPEAKSdouble(1:N,:,:);
  2 Kommentare
Oleg Komarov
Oleg Komarov am 20 Apr. 2011
Going from double to cell is unnecessary when you could use arrayfun instead of cellfun.
Padding could be done using the locations by creating a zero matrix and assigning only the peaks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by