如何让这个 for 循环在每次迭代时显示一个图形?

How to make this for loop shows a graph every iteration?

总体思路
我一直在做一个线性代数项目,其中的想法是测试一组给定的向量(矩阵)是否是线性的 dependent/independent。为此,下一个程序接收一个名为 value 的矩阵(user input/MxN),并首先通过标准(这部分没有问题)接下来,如果向量是线性相关的,它必须测试内部向量之间是否存在是一些 LI/LD 子集,为此它开始迭代对行进行排列并为其制定标准,如果这导致 LI 子集,它必须绘制向量和向量形成的 space 。即使初始矩阵的大小为 MxN,通常也期望矩阵为 2 或 3 列,R2 或 R3)。

问题
在第二遍中,一旦系统被标记为线性相关,系统就会在同一 windows 中重叠图形,所需的输出将是进行第一遍,如果系统是 LD,则显示初始图形,然后再显示开始绘制分离 windows 形成的置换矩阵的图形。

NewMatrix 遍历原始 "value" 矩阵并不断形成 rows/vector 的排列以再次检查标准(确实如此,但在同一个 window 中)。请注意,初始矩阵 "value" 由用户定义,并且应该已经在所示代码的起点处输入。

代码

RangS=rank(R)  //LI or ld criterion
[r, c] = size(value)
       if (rank(value))==r
            set(handles.text3,'String',('System>LI'));
            figure(3);
            hold on;
            z = zeros(size(value, 1), 1);
            quiver3(z, z, z, value(:, 1), value(:, 2), z, 0);
            grid on
            view(45, 45);
            s=sum(value);
            quiver3(0,0,0,s(1),s(2),0,'r');
            points=[X' Y'];


       else
           set(handles.text3,'String',('System>LD'));
           figure(3); //this graph should be done apart
           hold on;
           z = zeros(size(value, 1), 1);
           quiver3(z, z, z, value(:, 1), value(:, 2), z, 0);
           grid on
           view(45, 45);
           points=[X' Y'];
           for jj = 1:size(value,1)-1 //here starts permuting vectors>credits to MikeLimaOscar

                     for kk = jj+1:size(value,1)
                           NewMatrix= value([jj,kk],:)
                                F=rref(NewMatrix);
                                RangS=rank(R)  //the same criterion applied to the permutated matrices
                                [r, c] = size(NewMatrix)
                                if (rank(NewMatrix))==r
                                    set(handles.text3,'String',('Subsystem :LI'));
                                        figure(3); there  should be one graph for every permutated matrix
                                        hold on;
                                        z = zeros(size(NewMatrix, 1), 1);
                                        quiver3(z, z, z, NewMatrix(:, 1), NewMatrix(:, 2), z, 0);
                                        grid on
                                        view(45, 45);
                                        s=sum(NewMatrix);
                                        quiver3(0,0,0,s(1),s(2),0,'r');
                                        points=[X' Y'];


                               else
                                   set(handles.text3,'String',('Subsystem:LD'));
                                   figure(3);
                                   hold on;
                                   z = zeros(size(NewMatrix, 1), 1);
                                   quiver3(z, z, z, NewMatrix(:, 1), NewMatrix(:, 2), z, 0);
                                   grid on
                                   view(45, 45);
                                   points=[X' Y'];

                                end

                    end


end
       end     
  • 您正在同一 window [figure(3)] 上绘制所有图表。
  • 为图形提供不同的参数可以解决问题。

每个window

的特定索引
Permutation(jj) |Permutation 1   | Permutation 2   |  Permutation 3
____________________________________________________________________
                |[1]submatrix 1  | [4]submatrix 1  |[6]submatrix 1
submatrix(kk)   |[2]submatrix 2  | [5]submatrix 2  |[7]submatrix 2
                |[3]submatrix 3  |                 |[8]submatrix 3
                |                |                 |[9]submatrix 4
____________________________________________________________________
Last index      |     3          |       5         |     9       
____________________________________________________________________

括号中的索引将用作图形参数

  • Permutation 1, 只需使用子矩阵索引 kk, index_1 = kk
  • Permutation 2,使用子矩阵索引 kk 和来自置换 1
  • Last index 子矩阵
index_2 = Last index(Permutation 1) + kk
  • Permutation 3,使用子矩阵索引 kk 和来自置换 2
  • Last index 子矩阵
index_3 = Last index(Permutation 2) + kk

泛化,第一个排列的一部分,第n次排列中的索引为

index_n = Last index(Permutation n-1)) + kk

对于给定的问题 total of submatrices 对于给定的 Permutation jj 可以计算为

total(Permutation jj) = numel(jj+1:size(value,1))

请仔细阅读评论

% Check if the entire matrix is linear independent or not
if LI

    % Linear independent 
    % Plot the graph in window #1
    figure(1)

else 

    % Linear dependent 
    % Plot the graph in window #1
    figure(1) 

    % Starting index for next Permutation
    Last_index = 0;

    % Figure() argument initialization
    index = 0;

    % Permutation begins here
    for jj =  1:size(value,1)-1

        % submatrices for a given permutation jj begins here
        for kk = jj+1:size(value,1)

            % Check if submatrix is linear independent or not 
            if submatrix(kk) from permutation (jj) is LI

                % Linear independent 
                % Plot the graph in window #index
                index = Last_index + kk
                figure(index)

            else

                % Linear dependent 
                % Plot the graph in window #index
                index = Last_index + kk
                figure(index)

            end
            % End of checking if submatrix is linear independent or not 
        end

        % Update last index for the next permutation starting index
        Last_index = Last_index + numel(jj+1:size(value,1))

        % End of submatrices for a given permutation jj 

    end 
    % End of Permutation 
end
% End of checking if the entire matrix is linear independent or not