Add the resulting array of each loop to a cell

前端 未结 2 835
礼貌的吻别
礼貌的吻别 2021-01-28 11:21

I have some algorithm including a while loop:

 while  (condition)
    % do something and return a result array B
 end

Let\'s say:<

相关标签:
2条回答
  • 2021-01-28 11:42

    You can join your arrays either using horzcat or []. Then initialize an cell and save them into that cell.

    %% loop
    count = 0 ;
    iwant = cell([],1) ;
    while .....
            count = count+1 ;
        %% let be your arrays
        B1=1:9 ;
        B2=10:15 ;
        B3=16:19 ;
        B4=20:30 ;
        %% join them into array
        B = [B1 B2 B3 B4] ;
        iwant{count} = B ;
    end
    
    0 讨论(0)
  • 2021-01-28 11:55

    You can make use of the end keyword

    % Initialise empty cell array
    A = {};
    while *condition*
        % Create B using some calculation (different each loop)
        B = [1 2 3];
        % Other code ...
        % Assign to array
        A{end+1} = B;
    end
    
    0 讨论(0)
提交回复
热议问题