"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 = (i1)*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);
