Selecting Every Other Element in a Matrix

 Réponse acceptée

Star Strider
Star Strider le 16 Fév 2015
I’m not certain what you want, so I’ll venture two possibilities:
A=[1 2 8 7 6 5 4 6];
Aodd = A(1:2:end); % Odd-Indexed Elements
Aeven = A(2:2:end); % Even-Indexed Elements

7 commentaires

Dewey Phillips
Dewey Phillips le 26 Jan 2017
this worked for me
Aditya Kansal
Aditya Kansal le 30 Juil 2018
i want to do the same for a 2-D array, how can i achieve that?
Nitai Fingerhut
Nitai Fingerhut le 25 Déc 2018
A=[1 2 8; 7 6 5; 4 6 9];
A= A(:);
Aodd = A(1:2:end); % Odd-Indexed Elements
Aeven = A(2:2:end); % Even-Indexed Elements
at the end use reshpae to make it 2D again
Stephen23
Stephen23 le 25 Déc 2018
Modifié(e) : Stephen23 le 25 Déc 2018
"i want to do the same for a 2-D array, how can i achieve that?"
A(1:2:end,:) % odd rows
A(2:2:end,:) % even rows
A(:,1:2:end) % odd columns
A(:,2:2:end) % even columns
Very basic concepts, such as how to use indexing, are explained in the introductory tutorials:
Khyati Jain
Khyati Jain le 4 Juil 2019
How do you do this if it is a complex vector?
only for real number
Star Strider
Star Strider le 8 Fév 2024
It should work the same way for complex vectors, however the problem with that is that it could select only one of a complex pair. One way to correct for that would be to use the conj function to complete the pair after selecting them, then use the cplxpair function.
You would need to decide whether you wanted to select from complex vectors to begin with. The problems that could cause might not be worth the efffort.

Connectez-vous pour commenter.

Plus de réponses (1)

prashanth GT
prashanth GT le 2 Mar 2020

0 votes

function y = everyOther(x)
y = x(1:2:length(x));
end

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by