フィルターのクリア

How to save a vector from each iteration in a loop in a matrix?

11 ビュー (過去 30 日間)
Unnamed User
Unnamed User 2023 年 2 月 27 日
編集済み: Stephen23 2023 年 2 月 27 日
Essentially one of the outputs for each iteration of a for loop in my code is a row vector mesauring 1x300. After each iteration of the for loop, for 230 iterations, I'd like to save each row vector so that I end up with a matrix of 230x300, or essentially the 1x300 vector for each iteration value.
output_all(:,i) = output(1,:);
Is the best I've been able to come up with but only saves the final row vector for the loop. Any changes to this throw up errors such as, "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 300-by-1." when I use the following code:
output_all(:,i) = output(:,:);
I've already pre-allocated an empty matrix for the output from this:
output_all = zeros(size(230,300));
And help would be appreciated, I realise it's most likely a simple solution but I cannot seem to figure it out.

採用された回答

Stephen23
Stephen23 2023 年 2 月 27 日
編集済み: Stephen23 2023 年 2 月 27 日
Take a closer look at your preallocation. What does SIZE(..) return?:
size(230,300)
ans = 1
so your preallcoation
zeros(size(230,300))
ans = 0
is exactly equivalent to
zeros(1)
ans = 0
which returns a 1x1 matrix full of ... well, one single zero.
You probably intended this:
zeros(230,300)
ans = 230×300
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Tip: when debugging code, you need to look at what the code is really doing. The code does not care what you think it is doing, your task is to investigate what it is really doing. This means looking at values, classes, sizes, etc.
  1 件のコメント
Unnamed User
Unnamed User 2023 年 2 月 27 日
Thank you very much, got to be careful when I copy and paste code as I evidently forgot to remove size. All working now!

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

その他の回答 (1 件)

Torsten
Torsten 2023 年 2 月 27 日
output_all(:,i) = output(1,:).';
instead of
output_all(:,i) = output(1,:);
  1 件のコメント
Stephen23
Stephen23 2023 年 2 月 27 日
編集済み: Stephen23 2023 年 2 月 27 日
transposing the RHS makes no difference, MATLAB will automatically transpose vectors:
A = magic(3)
A = 3×3
8 1 6 3 5 7 4 9 2
A(:,1) = nan(1,3) % row vector
A = 3×3
NaN 1 6 NaN 5 7 NaN 9 2
A(:,2) = inf(3,1) % column vector
A = 3×3
NaN Inf 6 NaN Inf 7 NaN Inf 2

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

カテゴリ

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