extract elements and build the remain vector

조회 수: 19 (최근 30일)
Marko
Marko 2020년 12월 21일
댓글: Marko 2020년 12월 21일
Hello Community,
i need your help.
A vector v has 128 elements with random numbers.
The elements of index 1 21 31 41 should be etracted, and saved as vector a.
the remain elements of vector v should be saved as vector b.
my version consist two for loops which is in deed a bad solution for large vectors.
PS: the vectors are not constant.
maybe someone could finish the algorithm:
v = 1:128;
a = v([1 21 31 41]);

채택된 답변

Image Analyst
Image Analyst 2020년 12월 21일
Use setdiff():
N = 1600;
v = 1:N;
a = [2 59 87 113]
extracted = v(a)
otherIndexes = setdiff(1:length(v), a);
b = v(otherIndexes)

추가 답변 (1개)

David Hill
David Hill 2020년 12월 21일
a=v([1 21 31 41]);
b=v([2:20,22:30,32:40,42:end]);
  댓글 수: 5
David Hill
David Hill 2020년 12월 21일
b=v(~ismember(v,a));
Marko
Marko 2020년 12월 21일
Perfect!
You are a genius!

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by