Indexing sections of arrays

조회 수: 4 (최근 30일)
Mohammed Kagalwala
Mohammed Kagalwala 2019년 10월 23일
댓글: Mohammed Kagalwala 2019년 10월 23일
Is there an easy way to extract every section's of n elements of a vector in MATLAB and skip the next n? Say we have
x = linspace(1,10,10); %[1 2 3 4 5 6 7 8 9 10]
I wish to index such that I grab 1,2,3 then skip 4,5,6 and again grab 7,8,9. For example,
a = zeros(1,10); % [0 0 0 0 0 0 0 0 0 0]
a(magic index) = x(magic index)
So now a = [1 2 3 0 0 0 7 8 9 0]
I've only been able to find how to get every nth element but not chunks of n elements.
Thank you for your help in advance !
  댓글 수: 2
Jan w
Jan w 2019년 10월 23일
Define the indices you wish to extract
a = [1,2,3,7,8,9];
Now redefine your vector
x = x(a);
Is this what you mean?
Mohammed Kagalwala
Mohammed Kagalwala 2019년 10월 23일
Not exactly. I don't want a vector [1,2,3,7,8,9]. I want my 'a' vector to be [1 2 3 0 0 0 7 8 9 0]. Jos's answer below works as I wish !

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

채택된 답변

Jos (10584)
Jos (10584) 2019년 10월 23일
편집: Jos (10584) 2019년 10월 23일
x = 101:110
n = 3
tf = mod(0:numel(x)-1, 2*n) < n
a = zeros(size(x))
a(tf) = x(tf)
index = find(tf)
  댓글 수: 1
Mohammed Kagalwala
Mohammed Kagalwala 2019년 10월 23일
Thank you ! Works perfectly :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by