how to save the outputs obtained by a for loop repeated n times

5 ビュー (過去 30 日間)
Francesco Grechi
Francesco Grechi 2021 年 4 月 5 日
コメント済み: Francesco Grechi 2021 年 4 月 5 日
i built a for loop that works N times and in each loop produces as an output a matrix (6x3), i need to save every output in a matrix by column (6xN*3).
In my example the for loop is shown below and "tot" give to me only the last result of the loop.
PS. Just to let you knok "Tin" is a table that contains some data
function [Tout ] =Trasporta(Tin)
for group = 1:4:size(Tin,2)
[ordine(:,group), idx(:,group)] = sortrows(Tin{:,group});
end
% my question is about the for below
tot = nan(6,18); %N=6 N*3=18 the number of rows is 6, and the loop is repeated for 6 times
for i = 1:4:size(idx,2)
tot = (Tin{idx(:,i),i+1:i+3});
end
end

採用された回答

per isakson
per isakson 2021 年 4 月 5 日
編集済み: per isakson 2021 年 4 月 5 日
tot is overwritten in each iteration of the for-loop
Replace
tot = (Tin{idx(:,i),i+1:i+3});
by
tot(:,i) = (Tin{idx(:,i),i+1:i+3});
  3 件のコメント
per isakson
per isakson 2021 年 4 月 5 日
編集済み: per isakson 2021 年 4 月 5 日
Since I don't know your data I couldn't test. The indexing of tot was obviously wrong. Try
tot(:,i:i+2) = (Tin{idx(:,i),i+1:i+3});
Francesco Grechi
Francesco Grechi 2021 年 4 月 5 日
now it works

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

その他の回答 (0 件)

カテゴリ

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