riccs_0x
riccs_0x

Reputation: 201

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

General idea
I have been working on a linear algebra project, in which the idea is to test if a given set of vectors (a matrix) is linearly dependent/independent. For this the next program receives a matrix named value (user input/MxN), and make a first pass for the criterion (this part has no problems) next if the vectors are linearly dependent it must test if between the inner vectors there is some LI/LD subset, for this it starts iterating making permutations of the rows and making the criterion for it, if this results in LI subset, it must graph the vectors and the space formed by the vectors. Even when the size of the initial matrix is MxN, the matrix is usually expected to be 2 or 3 columns, R2 or R3).

Problem
In the second pass once the system is marked as linearly dependent, the system overlaps the graphics in the same windows , the desired output would be to make a first pass and if the system is LD show the a initial graph and later start graphing in separated windows the graphs of the permuted matrix formed.

NewMatrix iterates over the original "value" matrix and keeps forming the permutations of rows/vector to check the criterion again (it does, but in the same window). Note that the initial matrix "value" is defined by the user and is supposed to be already entered at the start point of the code showed.

Code

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     

Upvotes: 0

Views: 120

Answers (1)

Adam
Adam

Reputation: 2777

  • You're plotting all the graphs on the same window [figure(3)].
  • Providing Different arguments to figure can solve the problem.

Specific index for every 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       
____________________________________________________________________

The indices in brackets will be used as figure argument

  • Permutation 1, just use submatrix indices kk, index_1 = kk
  • Permutation 2, use submatrix indices kk and the Last index submatrices from permutation 1
index_2 = Last index(Permutation 1) + kk
  • Permutation 3, use submatrix indices kk and the Last index submatrices from permutation 2
index_3 = Last index(Permutation 2) + kk

Generalization, a part from the first permutation, the index in nth Permutation is

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

For the given question total of submatrices for a given Permutation jj can be computed as

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

Please read through the comments

% 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

Upvotes: 1

Related Questions