Selecting Every Other Element in a Matrix

조회 수: 135 (최근 30일)
Marshall Dunn
Marshall Dunn 2015년 2월 15일
댓글: Star Strider 2024년 2월 8일
How can I select every other element in a matrix, such as A=[1 2 8 7 6 5 4 6]?

채택된 답변

Star Strider
Star Strider 2015년 2월 16일
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
Atas Jyoti Kaibarta
Atas Jyoti Kaibarta 2022년 1월 21일
only for real number
Star Strider
Star Strider 2024년 2월 8일
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.

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

추가 답변 (1개)

prashanth GT
prashanth GT 2020년 3월 2일
function y = everyOther(x)
y = x(1:2:length(x));
end

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by