How to make the sizes of all variable same in the loop.

7 ビュー (過去 30 日間)
MakM
MakM 2022 年 1 月 4 日
コメント済み: MakM 2022 年 1 月 4 日
I have 4 variables, Game0, Game1, Game2, Game3 in the for loop. I am writing these variables to excel file by using the "writetable" function. All the variables size should be same if I am writing to the excel file. How can I do that. I am using Game1(numel(Game0))=0, but this is not correct because sometimes Game0 has no value.
  1 件のコメント
KSSV
KSSV 2022 年 1 月 4 日
See whose length is maximum ou t of four and append zeros to other variables.

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

採用された回答

KSSV
KSSV 2022 年 1 月 4 日
% Demo data
Game0 = rand(5,1) ;
Game1 = rand(7,1) ;
Game2 = rand(9,1) ;
Game3 = rand(3,1) ;
a = {Game0 Game1 Game2 Game3}; %test data
len = max(cellfun('length',a));
b = cellfun(@(x)[x;zeros(len-size(x,1),1)],a,'UniformOutput',false);
Game = [b{:}] ;
T = array2table(Game)
T = 9×4 table
Game1 Game2 Game3 Game4 _______ _______ _______ _______ 0.50908 0.3977 0.91829 0.52539 0.46445 0.28806 0.7916 0.56689 0.5089 0.95957 0.98846 0.34577 0.77494 0.40111 0.6214 0 0.38022 0.19804 0.37658 0 0 0.63789 0.1338 0 0 0.42852 0.80441 0 0 0 0.5414 0 0 0 0.49234 0
Note that there will be change in the variable name. Game1 in table is Game0.

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by