Matrix with variable number of rows and fixed columns
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
KE BR
am 12 Aug. 2020
Beantwortet: KE BR
am 12 Aug. 2020
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 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 Kommentare
Weitere Antworten (2)
KSSV
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!