Reduce a vector to the size of another vector

조회 수: 9 (최근 30일)
Noah Kilps
Noah Kilps 2020년 10월 6일
댓글: Noah Kilps 2020년 10월 6일
Hello,
I have two vectors currently. I have nonzeroDecel which is a 1x2168 double and start_1G which is a 1x7 double. I need to reduce nonzeroDecel so that for each number of start_1G, it takes the number that's closest and bigger than the number in start_1G. In the end I want nonzeroDecel to also be a 1x7. Any ideas? I have attached the variables from excel so you do not have to make up your own if you don't want to.

채택된 답변

KSSV
KSSV 2020년 10월 6일
편집: KSSV 2020년 10월 6일
Read about ismember, ismembertol, knsnearch.
If v is the vector and val is the value for which you want to get the closest value from v, use:
num = xlsread("matlab_question.xlsx") ;
vec = num(:,1) ;
val = num(:,2) ;
% Remove nans
val(isnan(val)) = [] ;
iwant = zeros(size(val)) ;
for i = 1:length(val)
[v,idx] = min(abs(vec-val(i))) ;
iwant(i) = vec(idx) ;
end
  댓글 수: 1
Noah Kilps
Noah Kilps 2020년 10월 6일
Thank you so much, this worked perfectly!!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by