Append to vector of different sizes in for loop

8 ビュー (過去 30 日間)
Jørgen Fone Pedersen
Jørgen Fone Pedersen 2021 年 2 月 14 日
編集済み: dpb 2021 年 2 月 15 日
Hi.
How does one append a vector to a another vector within a loop.
I have the code below: Point is i want the signal between the first and second indices of ipts. then the third and fourth et cetera up to the nineteenth to twentieth.
with this code I only get the last part nineteenth to twentieth.
i = 1;
while i < 19;
A = TimeSeries_short(ipts(i):ipts(i+1));
i = i+2;
end
.mat file is attached.

採用された回答

Stephen23
Stephen23 2021 年 2 月 15 日
Simpler:
S = load('test_file.mat')
S = struct with fields:
TimeSeries_short: [1×47000 single] ipts: [1182 3977 5678 8607 10129 13100 14657 17610 19172 22121 23650 26578 28197 31090 32741 35696 37300 40136 41691 44808]
fun = @(b,e)S.TimeSeries_short(b:e);
out = arrayfun(fun,S.ipts(1:2:end),S.ipts(2:2:end),'uni',0)
out = 1x10 cell array
{1×2796 single} {1×2930 single} {1×2972 single} {1×2954 single} {1×2950 single} {1×2929 single} {1×2894 single} {1×2956 single} {1×2837 single} {1×3118 single}
  1 件のコメント
Jørgen Fone Pedersen
Jørgen Fone Pedersen 2021 年 2 月 15 日
Thank you.
This works great

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

その他の回答 (1 件)

dpb
dpb 2021 年 2 月 14 日
The short answer is
i = 1;
A=[];
for i=1:2:numel(ipts)
A = [A;TimeSeries_short(ipts(i):ipts(i+1),:);
end
Normally one frowns on dynamic reallocation, but presuming the overall array is going to be relatively small, the time taken won't be excessive.
If A is going to be very large, then one will want to calculate the final size and compute the indices going in and explicitly set the rows.
  6 件のコメント
dpb
dpb 2021 年 2 月 15 日
編集済み: dpb 2021 年 2 月 15 日
OK, I presumed a column-wise array, not a vector.
Hence I referenced
T(i1:i2,:)
to pull all rows of the array T, not just a single column. For a vector need just the 1D indexing expression.
A = [A;TimeSeries_short(ipts(i):ipts(i+1))];
instead for a vector (column output); replace semi-colon with comma for row output.
Jørgen Fone Pedersen
Jørgen Fone Pedersen 2021 年 2 月 15 日
ahh, thank you.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by