フィルターのクリア

How to sequentially generate a vector from loop?

1 回表示 (過去 30 日間)
Ani Asoyan
Ani Asoyan 2023 年 1 月 22 日
コメント済み: Dyuman Joshi 2023 年 1 月 22 日
Hi everyone!
I have a vector A, which I cut in a loop and take the first elements like this
A = [ 1 2 3 4 5 6 7]'
for k=1:7
A_{k} = A(1:k)
end
I want to generate a vector B which can put a subset of A (let's say the first 2 elements (A_{2}')) together with the last element of every generated vector from my loop. Basically B should be like this
B = [A_{2}' A_{3}(3) A_{4}(4) A_{5}(5) A_{6}(6) A_{7}(7)]'
How can I make this vector B easier to write for bigger k's (for example write it like a loop) ?
Thank you so much!
  3 件のコメント
Ani Asoyan
Ani Asoyan 2023 年 1 月 22 日
With this loop it's same, but if there's another function in the loop, it's not the same. I just want to create a vector B, which contains first 2 elements of A and every time it adds the last element of a for loop
Dyuman Joshi
Dyuman Joshi 2023 年 1 月 22 日
"but if there's another function in the loop"
For example?
"I just want to create a vector B, which contains first 2 elements of A and every time it adds the last element of a for loop"
Give another example, as it is not clear from the one you gave.

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

回答 (1 件)

Sargondjani
Sargondjani 2023 年 1 月 22 日
It's not clear what exaclty is your problem. But maybe something like this can help you:
s=2
for k=1:7
A_{k} = A(1:k);
if k==s
B = [A_{2}']
elseif k>s
B = [B' A_{k}(k)]'
end
end
Try to get the details right yourself.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by