Separate out every fourth element of a Vector

9 ビュー (過去 30 日間)
Tom
Tom 2019 年 9 月 13 日
コメント済み: Tom 2019 年 9 月 13 日
I have a 400x1 vector, I essentially need to take the first 3 elements from the vector and put them in a new vector, skip the fourth element, then take elements 5, 6, 7 and skip the eighth, and so on on, until I have a 300x1 vector.
This is slightly more complicated than it sounds, because obviously once you take away an element everything else is going to shift up, so you have to make it clear that this is not the intention.

採用された回答

James Tursa
James Tursa 2019 年 9 月 13 日
編集済み: James Tursa 2019 年 9 月 13 日
V = your vector
result = V;
result(4:4:end) = []; % remove every 4th element
The above syntax with [] on the rhs is special notation for removing the indexed elements on the lhs.

その他の回答 (1 件)

Steven Lord
Steven Lord 2019 年 9 月 13 日
If by " so you have to make it clear that this is not the intention." you mean you don't want to delete every fourth element but to replace them with some placeholder, use missing or NaN (if your data is double or single precision.)
A = 1:20;
B = A;
B(4:4:end) = missing
C = A;
C(4:4:end) = NaN
If you want to eliminate every fourth element entirely, use James Tursa's solution.
  1 件のコメント
Tom
Tom 2019 年 9 月 13 日
Yes, Tursa's method was what I tried before but I thought it was wrong as the code is not producing the right solution, but that method is right it seems so something else must be wrong.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by