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

조회 수: 1 (최근 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 CenterFile Exchange에서 Number games에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by