Matrix with variable number of rows and fixed columns
조회 수: 11 (최근 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에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!