Selecting Every Other Element in a Matrix

137 vues (au cours des 30 derniers jours)
Marshall Dunn
Marshall Dunn le 15 Fév 2015
Commenté : Star Strider le 8 Fév 2024
How can I select every other element in a matrix, such as A=[1 2 8 7 6 5 4 6]?

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
Atas Jyoti Kaibarta
Atas Jyoti Kaibarta le 21 Jan 2022
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
function y = everyOther(x)
y = x(1:2:length(x));
end

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by