Matrix with variable number of rows and fixed columns
26 次查看(过去 30 天)
显示 更早的评论
Hi
I am having trouble with some code. I have a mxn matrix BT where for each column i want to extract some data.
[H,W] = size(BT);
for i= 1:W
[x(:,i),y(:,i)] = findpeaks((BT(:,i)),f1)
end
What this does is find the peaks for the first column of BT and store the amplitude into X and the frequency into Y and then move on to the second column. The problem is that lets say for the first column i get 19values wich are stored into X and Y. But for the second column i get 20values wich can not be stored into X and Y because the number of rows are not equal.
I was thinking to add zeros to a fixed number of rows for each column but i dont know how to do it for this code. Can anybody help me with this problem?
Kind regards
0 个评论
采纳的回答
Walter Roberson
2020-8-12
This code uses nan padding instead of 0 padding, in case the peak value happens to be 0. If you know that cannot happen, then the code could be made a bit more simple.
[H,W] = size(BT);
x = []; y = [];
nr = 0;
for i = 1:W
[thisx, thisy] = findpeaks((BT(:,i)),f1);
nx = length(thisx);
if nx <= nr
thisx(end+1:nr) = nan;
thisy(end+1:nr) = nan;
else
x(end+1:nx, :) = nan;
y(end+1:nx, :) = nan;
end
x(:, i) = thisx;
y(:, i) = thisy;
nr = size(x,1);
end
0 个评论
更多回答(2 个)
KSSV
2020-8-12
As you will have variable number of values in each column, save them in the cell.
[H,W] = size(BT);
x = cell(W,1) ; y = cell(W,1) ;
for i= 1:W
[xx,yy] = findpeaks((BT(:,i)),f1)
x{i} = xx ;y{i} = yy ;
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!