take the first and last row of a matrix but sample in between

Hi. I have created a 501 x 7 matrix, and would like to downsample the matrix to be 15 x 7. However, I would like the first and last rows to remain the same while sampling in between. Is there an easy way to do this?
I attached the function and have the code below that I am using to get my matrix.
spline_basis_obj = create_bspline_basis(15, 7, 4); % create spline basis set
figure;plot(spline_basis_obj); % plot the object
h = findobj(gca,'Type','line'); % get details on the lines
x=get(h,'Xdata'); % get x values on line (all the same values)
y=get(h,'Ydata'); % get the y values for each line
tempdata = cell2mat(y(4:end))'; % remove the [0,1] terms and make into matrix
spline_basis = % downsample columns to 15 rows here while keeping the first and last rows the same

 채택된 답변

the cyclist
the cyclist 2021년 1월 30일
If you have the Statistics and Machine Learning Toolbox, the randsample function is handy for this:
% Original data
A = rand(501,7);
% 13 random integers from 2 to 500 (without replacement)
idx = 1 + randsample(499,13,false);
% Pull these rows from A, and the first and last
B = A([1; sort(idx); end],:);
If you don't, then you can use randi instead of randsample, but you'll need to check for duplicate indices. (You could, for example, generate several more than you need, and just take the first 13 unique ones.)

댓글 수: 2

Thanks! Almost, but not quite it! I did not want the sampling to be totally random. I apologize for not describing that. I actually want to sample uniformly across.
So it would be keep the first and last rows, and then sample every 38 or 39 integers (in the case of 499 total integers).
OK. I think if you replace idx with
idx = (2:ceil(499/13):500)';
then you'll get what you want.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Splines에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2021년 1월 30일

댓글:

2021년 1월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by