getting even elements from vector

26 visualizzazioni (ultimi 30 giorni)
avner yakov
avner yakov il 30 Dic 2016
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

Risposte (1)

KSSV
KSSV il 30 Dic 2016
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 Commenti
Om Yadav
Om Yadav il 5 Apr 2020
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
That is a good way to think KSSV

Accedi per commentare.

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by