Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Indexing_my_result_matrix......for_loop_problem

Subject: Indexing_my_result_matrix......for_loop_problem

From: Kurt

Date: 10 Dec, 2010 17:31:06

Message: 1 of 4

Hello everyone,

My code at the end of my post. Basically, it produces a matrix at the end which I call 'result'. The contents inside the 'result' matrix is dependent on some conditions I set.

My problem is that I cant get a final 'result' matrix to store all my previous 'results' as the for loop progresses. I know that the'results' matrix has to indexed, I tried but I just can't seem to get it working.

Please guys, I'm asking for some help if possible.
--Kurtis

%---------------------------------------------------------------------------------------------------------------------
clc;
clear all;
close all;

Lines =[ 0 0 857.179816392243 2481.23697255084
                  866.3456 2468.331456 798.89536 2563.30208
                 851.89504 2199.971264 854.63936 2346.02912
                         0 0 1013.14632938022 2223.85360462265
               1187.868416 1999.438656 1018.6768 2216.750208
               1009.367552 2036.984384 1013.008064 2217.016064
                         0 0 2263.76391898131 2731.83299405823
                   2270.88 2741.06112 2394.11712 2900.87488
                2264.79104 2674.98304 2262.9472 2777.03744
                         0 0 577.131144648514 2106.43625165764
                  585.4816 2102.1195328 472.66432 2160.4397632
                 577.07712 1958.981376 577.12 2076.018048]
                 
                 
    [nrow,ncol]=size(Lines)
    rowsPerIter = 3 ;

    for i = 1:nrow/3
    
           from = (i-1)*rowsPerIter+1
       
    P1L1(i,:) = [Lines(from,1) Lines(from,2)];
    P1L2(i,:) = [Lines(from+1,1) Lines(from+1,2)];
    P2L2(i,:) = [Lines(from+1,3) Lines(from+1,4)];
    
    P2L1(i,:) = [Lines(from,3) Lines(from,4)];
    P1L3(i,:) = [Lines(from+2,1) Lines(from+2,2)];
    P2L3(i,:) = [Lines(from+2,3) Lines(from+2,4)];
    
    x1(i) = Lines(from,1);
    y1(i) = Lines(from,2);
    x2(i) = Lines(from,3);
    y2(i) = Lines(from,4);
    x3(i) = Lines(from+1,1);
    y3(i) = Lines(from+1,2);
    x4(i) = Lines(from+1,3);
    y4(i) = Lines(from+1,4);
    x5(i) = Lines(from+2,1);
    y5(i) = Lines(from+2,2);
    x6(i) = Lines(from+2,3);
    y6(i) = Lines(from+2,4);
    
     Euclid_dist_of_P2L1_and_P1L2(i) = sqrt (( x2(i) - x1(i) )^2 + (y2(i) -y1(i) )^2) ;
     Euclid_dist1_of_P2L1_and_P2L2(i) = sqrt ((x4(i) - x2(i) )^2 + (y4(i) -y2(i) )^2) ;
     Euclid_dist_of_P2L1_and_P1L3(i) = sqrt ((x5(i) - x2(i) )^2 + (y5(i) -y2(i) )^2) ;
     Euclid_dist1_of_P2L1_and_P2L3(i) = sqrt ((x6(i) - x2(i) )^2 + (y6(i) -y2(i) )^2) ;

if Euclid_dist_of_P2L1_and_P1L2(i) < Euclid_dist1_of_P2L1_and_P2L2(i)
    
           P1L2(i,:) = P2L1(i,:) ;
           
else
           P2L2(i,:) = P2L1(i,:) ;
end

if Euclid_dist_of_P2L1_and_P1L3(i) < Euclid_dist1_of_P2L1_and_P2L3(i)
    
           P1L3(i,:) = P2L1(i,:) ;
           
else
           P2L3(i,:) = P2L1(i,:) ;
end

if (x5(i) < x2(i) && x2(i) < x6(i) || x5(i) > x2(i) && x2(i) > x6(i) ) && (y5(i) < y2(i) && y2(i) < y6(i) || y5(i) > y2(i) && y2(i) > y6(i) )
 P1L3(i,:) = [Lines(from+2,1) Lines(from+2,2)];
 P2L3(i,:) = [Lines(from+2,3) Lines(from+2,4)];
 

 
                                                      result= [ P1L2(i,:) P2L2(i,:);
                                                                     P2L1(i,:) P1L3(i,:) ;
                                                                      P1L2(i,:) P2L2(i,:);
                                                                     P2L1(i,:) P2L3(i,:) ]
 
else
    if (x3(i) < x2(i) && x2(i) < x4(i) || x3(i) > x2(i) && x2(i) > x4(i) ) && (y3(i) < y2(i) && y2(i) < y4(i) || y3(i) > y2(i) && y2(i) > y4(i) )
     P1L2(i,:) = [Lines(from+1,1) Lines(from+1,2)];
    P2L2(i,:) = [Lines(from+1,3) Lines(from+1,4)];
    
                                                   result= [ P1L3(i,:) P2L3(i,:);
                                                                     P2L1(i,:) P1L2(i,:) ;
                                                                      P1L3(i,:) P2L3(i,:);
                                                                     P2L1(i,:) P2L2(i,:) ]
   
    else
        
                               result= [ P1L2(i,:) P2L2(i,:) ;
                                               P1L3(i,:) P2L3(i,:) ]
     
    end
        
end
                                              
    end
%----------------------------------------------------------------------------------------------------------

Subject: Indexing_my_result_matrix......for_loop_problem

From: Sean de

Date: 10 Dec, 2010 18:11:05

Message: 2 of 4

"Kurt " <rerty258@gmail.com> wrote in message <idto4q$roj$1@fred.mathworks.com>...
> Hello everyone,
>
> My code at the end of my post. Basically, it produces a matrix at the end which I call 'result'. The contents inside the 'result' matrix is dependent on some conditions I set.
>
> My problem is that I cant get a final 'result' matrix to store all my previous 'results' as the for loop progresses. I know that the'results' matrix has to indexed, I tried but I just can't seem to get it working.
>
> Please guys, I'm asking for some help if possible.
> --Kurtis
>
> %---------------------------------------------------------------------------------------------------------------------
> clc;
> clear all;
> close all;
>
> Lines =[ 0 0 857.179816392243 2481.23697255084
> 866.3456 2468.331456 798.89536 2563.30208
> 851.89504 2199.971264 854.63936 2346.02912
> 0 0 1013.14632938022 2223.85360462265
> 1187.868416 1999.438656 1018.6768 2216.750208
> 1009.367552 2036.984384 1013.008064 2217.016064
> 0 0 2263.76391898131 2731.83299405823
> 2270.88 2741.06112 2394.11712 2900.87488
> 2264.79104 2674.98304 2262.9472 2777.03744
> 0 0 577.131144648514 2106.43625165764
> 585.4816 2102.1195328 472.66432 2160.4397632
> 577.07712 1958.981376 577.12 2076.018048]
>
>
> [nrow,ncol]=size(Lines)
> rowsPerIter = 3 ;
>
> for i = 1:nrow/3
>
> from = (i-1)*rowsPerIter+1
>
> P1L1(i,:) = [Lines(from,1) Lines(from,2)];
> P1L2(i,:) = [Lines(from+1,1) Lines(from+1,2)];
> P2L2(i,:) = [Lines(from+1,3) Lines(from+1,4)];
>
> P2L1(i,:) = [Lines(from,3) Lines(from,4)];
> P1L3(i,:) = [Lines(from+2,1) Lines(from+2,2)];
> P2L3(i,:) = [Lines(from+2,3) Lines(from+2,4)];
>
> x1(i) = Lines(from,1);
> y1(i) = Lines(from,2);
> x2(i) = Lines(from,3);
> y2(i) = Lines(from,4);
> x3(i) = Lines(from+1,1);
> y3(i) = Lines(from+1,2);
> x4(i) = Lines(from+1,3);
> y4(i) = Lines(from+1,4);
> x5(i) = Lines(from+2,1);
> y5(i) = Lines(from+2,2);
> x6(i) = Lines(from+2,3);
> y6(i) = Lines(from+2,4);
>
> Euclid_dist_of_P2L1_and_P1L2(i) = sqrt (( x2(i) - x1(i) )^2 + (y2(i) -y1(i) )^2) ;
> Euclid_dist1_of_P2L1_and_P2L2(i) = sqrt ((x4(i) - x2(i) )^2 + (y4(i) -y2(i) )^2) ;
> Euclid_dist_of_P2L1_and_P1L3(i) = sqrt ((x5(i) - x2(i) )^2 + (y5(i) -y2(i) )^2) ;
> Euclid_dist1_of_P2L1_and_P2L3(i) = sqrt ((x6(i) - x2(i) )^2 + (y6(i) -y2(i) )^2) ;
>
> if Euclid_dist_of_P2L1_and_P1L2(i) < Euclid_dist1_of_P2L1_and_P2L2(i)
>
> P1L2(i,:) = P2L1(i,:) ;
>
> else
> P2L2(i,:) = P2L1(i,:) ;
> end
>
> if Euclid_dist_of_P2L1_and_P1L3(i) < Euclid_dist1_of_P2L1_and_P2L3(i)
>
> P1L3(i,:) = P2L1(i,:) ;
>
> else
> P2L3(i,:) = P2L1(i,:) ;
> end
>
> if (x5(i) < x2(i) && x2(i) < x6(i) || x5(i) > x2(i) && x2(i) > x6(i) ) && (y5(i) < y2(i) && y2(i) < y6(i) || y5(i) > y2(i) && y2(i) > y6(i) )
> P1L3(i,:) = [Lines(from+2,1) Lines(from+2,2)];
> P2L3(i,:) = [Lines(from+2,3) Lines(from+2,4)];
>
>
>
> result= [ P1L2(i,:) P2L2(i,:);
> P2L1(i,:) P1L3(i,:) ;
> P1L2(i,:) P2L2(i,:);
> P2L1(i,:) P2L3(i,:) ]
>
> else
> if (x3(i) < x2(i) && x2(i) < x4(i) || x3(i) > x2(i) && x2(i) > x4(i) ) && (y3(i) < y2(i) && y2(i) < y4(i) || y3(i) > y2(i) && y2(i) > y4(i) )
> P1L2(i,:) = [Lines(from+1,1) Lines(from+1,2)];
> P2L2(i,:) = [Lines(from+1,3) Lines(from+1,4)];
>
> result= [ P1L3(i,:) P2L3(i,:);
> P2L1(i,:) P1L2(i,:) ;
> P1L3(i,:) P2L3(i,:);
> P2L1(i,:) P2L2(i,:) ]
>
> else
>
> result= [ P1L2(i,:) P2L2(i,:) ;
> P1L3(i,:) P2L3(i,:) ]
>
> end
>
> end
>
> end
> %----------------------------------------------------------------------------------------------------------


Way too much code to look through so try this:
%Add this line at the very beginning
result = cell(length(1:nrow/3),1)

%Then in the for loop when you define result:
result{i} = [ P1L2(i,:) P2L2(i,:) ;
                      P1L3(i,:) P2L3(i,:) ]

%And after the loop:
result = cell2mat(result);

Subject: Indexing_my_result_matrix......for_loop_problem

From: Kurt

Date: 10 Dec, 2010 18:54:20

Message: 3 of 4

Thanks again sean.

I have a question though, why should one bother would indexing output matrices in for loops , if cell storing is a more elegant way to go?

Is it because of code speed issues?

--kurt

Subject: Indexing_my_result_matrix......for_loop_problem

From: Sean de

Date: 10 Dec, 2010 19:21:06

Message: 4 of 4

"Kurt " <rerty258@gmail.com> wrote in message <idtt0s$qhf$1@fred.mathworks.com>...
> Thanks again sean.
>
> I have a question though, why should one bother would indexing output matrices in for loops , if cell storing is a more elegant way to go?
>
> Is it because of code speed issues?
>
> --kurt

Cell storing isn't necessarily more elegant, nor is it faster. However, if you're having troubles figuring out indexing areas it's far easier.

In your case you had to set a 2d matrix to a chunk of your result. This would require knowing the starting point and ending point (or length) in each dimension and referencing this all directly. For speed you would've had to preallocate this entire result matrix to begin with. Instead, as long as the size is the same (in the non-stacking direction (e.g. 2x3 and 11x3 would be okay but 2x4,2x5 would not be since we're stacking vertically)) for every chunk you can store each chunk as its own, well I guess chunk, in the cell array and then assemble it at the end.

-Sean

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us