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
0 个评论
采纳的回答
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,:,:);
2 个评论
Oleg Komarov
2011-4-20
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!