extract elements and build the remain vector

8 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

类别

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