Matrix with variable number of rows and fixed columns

27 Ansichten (letzte 30 Tage)
KE BR
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

Akzeptierte Antwort

Walter Roberson
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

Weitere Antworten (2)

KSSV
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

KE BR
KE BR am 12 Aug. 2020
stephen, KSSV, Walter all of your answers works perfectly! :) Thanks for your help and time! I had to accept Walter's answer as for future work it will be easier for me to work with a fixed number of rows and columns wich his code does! nevertheless thanks for the time and help! its much appreciated. :)

Community Treasure Hunt

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

Start Hunting!

Translated by