MATLAB Newsgroup

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

%----------------------------------------------------------------------------------------------------------

"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);

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

"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

You can think of your watch list as threads that you have bookmarked.

You can add tags, authors, threads, and even search results to your watch list. This way you can easily keep track of topics that you're interested in. To view your watch list, click on the "My Newsreader" link.

To add items to your watch list, click the "add to watch list" link at the bottom of any page.

To add search criteria to your watch list, search for the desired term in the search box. Click on the "Add this search to my watch list" link on the search results page.

You can also add a tag to your watch list by searching for the tag with the directive "tag:tag_name" where tag_name is the name of the tag you would like to watch.

To add an author to your watch list, go to the author's profile page and click on the "Add this author to my watch list" link at the top of the page. You can also add an author to your watch list by going to a thread that the author has posted to and clicking on the "Add this author to my watch list" link. You will be notified whenever the author makes a post.

To add a thread to your watch list, go to the thread page and click the "Add this thread to my watch list" link at the top of the page.

*No tags are associated with this thread.*

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.

The newsgroups are a worldwide forum that is open to everyone. Newsgroups are used to discuss a huge range of topics, make announcements, and trade files.

Discussions are threaded, or grouped in a way that allows you to read a posted message and all of its replies in chronological order. This makes it easy to follow the thread of the conversation, and to see what’s already been said before you post your own reply or make a new posting.

Newsgroup content is distributed by servers hosted by various organizations on the Internet. Messages are exchanged and managed using open-standard protocols. No single entity “owns” the newsgroups.

There are thousands of newsgroups, each addressing a single topic or area of interest. The MATLAB Central Newsreader posts and displays messages in the comp.soft-sys.matlab newsgroup.

**MATLAB Central**

You can use the integrated newsreader at the MATLAB Central website to read and post messages in this newsgroup. MATLAB Central is hosted by MathWorks.

Messages posted through the MATLAB Central Newsreader are seen by everyone using the newsgroups, regardless of how they access the newsgroups. There are several advantages to using MATLAB Central.

**One Account**

Your MATLAB Central account is tied to your MathWorks Account for easy access.

**Use the Email Address of Your Choice**

The MATLAB Central Newsreader allows you to define an alternative email address as your posting address, avoiding clutter in your primary mailbox and reducing spam.

**Spam Control**

Most newsgroup spam is filtered out by the MATLAB Central Newsreader.

**Tagging**

Messages can be tagged with a relevant label by any signed-in user. Tags can be used as keywords to find particular files of interest, or as a way to categorize your bookmarked postings. You may choose to allow others to view your tags, and you can view or search others’ tags as well as those of the community at large. Tagging provides a way to see both the big trends and the smaller, more obscure ideas and applications.

**Watch lists**

Setting up watch lists allows you to be notified of updates made to postings selected by author, thread, or any search variable. Your watch list notifications can be sent by email (daily digest or immediate), displayed in My Newsreader, or sent via RSS feed.

- Use a newsreader through your school, employer, or internet service provider
- Pay for newsgroup access from a commercial provider
- Use Google Groups
- Mathforum.org provides a newsreader with access to the comp.soft sys.matlab newsgroup
- Run your own server. For typical instructions, see: http://www.slyck.com/ng.php?page=2