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

1 次查看(过去 30 天)
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

采纳的回答

Andrei Bobrov
Andrei Bobrov 2011-4-20
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,:,:);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by