getting even elements from vector

조회 수: 24 (최근 30일)
avner yakov
avner yakov 2016년 12월 30일
i'm trying to create new vector from another vector with only the odd elements of the of vectorr. i tried this:
for i=length(q1)
if mod(i,2)==0
a1(i,:)=q1(i,:);
else
a1(i,:)=[];
end
end
when q1 is the old vector and a1 is the new one and q1 is a column vector so i'm trying to duplicate his even row's

답변 (1개)

KSSV
KSSV 2016년 12월 30일
You can extract the even indices and odd indices at one stretch without loop.
k = rand(1,10) ;
% even index values
iwant_even = k(2:2:end) ;
% odd index values
iwant_odd = k(1:2:end) ;
  댓글 수: 3
Om Yadav
Om Yadav 2020년 4월 5일
This is a good answer. This is what I was looking for. A word of advice, avoid using for loop as much as possible in MATLAB, make use of vectorization instead. MATLAB is made for this.
carlos edurdo condori ochoa
carlos edurdo condori ochoa 2020년 8월 28일
That is a good way to think KSSV

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

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by