Matrix with variable number of rows and fixed columns
26 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Walter Roberson
on 12 Aug 2020
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 Comments
More Answers (2)
KSSV
on 12 Aug 2020
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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!