??? Error using ==> zeros

Maximum variable size allowed by the program is exceeded.

Error in ==> pigsfinal1 at 125

K1 = zeros(Npts_P1-1,Npts_P2);

Can someone tell me how do i solve the problem so that i can run this program.

this is my codings,

function pigs();

rgb1=imread('aa.jpeg');

BW = im2bw(rgb1,0.4);

figure,imshow(BW);

rgb2=imread('bb.jpeg');

BW2 = im2bw(rgb2,0.4);

figure,imshow(BW2);

rgb3=imread('cc1.jpeg');

BW3 = im2bw(rgb3,0.4);

figure,imshow(BW3);

%figure;

%BWforimg1 = bwareaopen(BW,500,8);

% to remove unwanted or some area from the image

se = strel('disk',35);

BWforimg1 = imopen(BW,se);

figure, imshow(BWforimg1), title('Img1 After opening')

BWforimg2 = imopen(BW2,se);

figure, imshow(BWforimg2), title('Img2 After opening')

BWforimg3 = imopen(BW3,se);

figure, imshow(BWforimg3), title('Img3 After opening')

% two find number of object as cluster

[L1,num1]=bwlabel(BWforimg1,8);

num1

[L2,num2]=bwlabel(BWforimg2,8);

num2

[L3,num3]=bwlabel(BWforimg3,8);

num3

%L1 = watershed(bw2,8);

%[D,L] = bwdist(L1,'euclidean');

% to get the indices of first pig in img

[r,c]=find(L2==1);%change

%P=[r , c ];

P1 = struct('x', r, 'y', c);

tf1 = isstruct(P1)

% to get the indices of second pig in img

[m,n]=find(L2==2); %change

P2 = struct('x', m, 'y', n);

tf2 = isstruct(P2)

%P2 = [m n];

%[m2,n2] = size(P2);

%to view the boundary of the pigs in image

figure;

[C1,h1]=imcontour(BWforimg1);

figure;

[C2,h2]=imcontour(BWforimg2,3);

figure;

[C3,h3]=imcontour(BWforimg3);

%[D1,L1] = bwdist(BWforimg1,'euclidean');

%[D2,L2] = bwdist(BWforimg2,'euclidean');

%[D3,L3] = bwdist(BWforimg3,'euclidean');

%calculate minumin distace

Npts_P1=numel(P1.x);

Npts_P2=numel(P2.x);

if Npts_P1>1 && Npts_P2>1

% Bounding box test

minP1x = min(P1.x);

maxP1x = max(P1.x);

minP1y = min(P1.y);

maxP1y = max(P1.y);

minP2x = min(P2.x);

maxP2x = max(P2.x);

minP2y = min(P2.y);

maxP2y = max(P2.y);

% if ((minP1x < maxP2x) && (maxP1x > minP2x) && ...

% (minP1y < maxP2y) && (maxP1y > minP2y))

% Checking if any of the segments of the 2 polygons are crossing

% S. H?lz function

%[x,y] = curveintersect(P1.x,P1.y,P2.x,P2.y);

% if ~isempty(x)

% min_d = 0;

% figure

% hold on

% box on

% axis equal

% plot(P1.x,P1.y,P2.x,P2.y);

% plot(x,y,'ro');

% return;

%end

%end

end

% To compute the minimum distance between two polygons, we compute the

% minimum distance between all vertices and all edges.

% To compute this distance, one needs to compute the minimum distance

% between a vertex I (xi,yi) and an edge [A B] of

% coordinates (xa,ya) and (xb,yb)

% To compute this distance, one computes the projected point of the vertex

% I on the line passing through the vertices of the edge [A B].

% The projected point P corresponds to the minimum distance between the

% vertex and the line.

% The coordiantes of point P are computed in a parametric way with the line

% created by points A and B:

if Npts_P1 >= 3

dP1x = diff(P1.x);

dP1y = diff(P1.y);

sdP12 = dP1x.^2 + dP1y.^2;

if any(sdP12 == 0)

warning('min_dist_between_two_pigs:w1',...

'Two successive points of P1 are indentical.')

end

% there are bug in matlab when create zeros matrix with high dimention

K1 = zeros(Npts_P1-1,Npts_P2);

D1 = zeros(Npts_P1-1,Npts_P2);

for i = 1:numel(P1.x)-1

% Compute the closest distance between a point and a segment

% Computation of the parameter k

k = -(dP1x(i) * (P1.x(i) - P2.x) +...

dP1y(i) * (P1.y(i) - P2.y)) / sdP12(i);

I1 = k < 0;

I2 = k > 1;

I =~(I1|I2);

% Computation of the minimum squared distances between all points of

% P2 and the i-th segment of P1.

D1(i,I1) = (P2.x(I1)- P1.x(i )).^2+(P2.y(I1)-P1.y(i )).^2;

D1(i,I2) = (P2.x(I2)- P1.x(i+1)).^2+(P2.y(I2)-P1.y(i+1)).^2;

D1(i,I ) = (P2.x(I )-(P1.x(i)+k(I)*dP1x(i))).^2+...

(P2.y(I )-(P1.y(i)+k(I)*dP1y(i))).^2;

K1(i,:) = k;

end

% One computes the minimum distance for pig 1

[min_d1,ind_seg1] = min(D1);

[min_D1,ind_pt1 ] = min(min_d1);

end

%%%%%%%%%%%%%%%%%%%%%%%%

% The same operations are performed for all segments of P2.

if Npts_P2 >= 3

dP2x = diff(P2.x);

dP2y = diff(P2.y);

sdP22 = dP2x.^2 + dP2y.^2;

if any(sdP22 == 0)

warning('min_dist_between_two_pig:w2',...

'Two successive points of P1 are indentical.')

end

% there are bug in matlab when create zeros matrix with high dimention

K2 = zeros(Npts_P2-1,Npts_P1);

D2 = zeros(Npts_P2-1,Npts_P1);

for i = 1:Npts_P2-1

k = -(dP2x(i) * (P2.x(i) - P1.x) + ...

dP2y(i) * (P2.y(i) - P1.y))/sdP22(i);

I1 = k < 0;

I2 = k > 1;

I =~(I1|I2);

D2(i,I1) = (P1.x(I1)-P2.x(i )).^2+(P1.y(I1)-P2.y(i )).^2;

D2(i,I2) = (P1.x(I2)-P2.x(i+1)).^2+(P1.y(I2)-P2.y(i+1)).^2;

D2(i,I ) = (P1.x(I )-(P2.x(i)+k(I)*dP2x(i))).^2+...

(P1.y(I )-(P2.y(i)+k(I)*dP2y(i))).^2;

K2(i,:) = k;

end

[min_d2,ind_seg2] = min(D2);

[min_D2,ind_pt2 ] = min(min_d2);

end

if (Npts_P1 >= 3) && (Npts_P2 >= 3)

[min_d,ind_tot] = min([min_D1,min_D2]);

min_d = sqrt(min_d);

elseif Npts_P1 < 3

ind_tot = 2;

min_d = sqrt(min_D2);

elseif Npts_P2 < 3

ind_tot = 1;

min_d = sqrt(min_D1);

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure

% hold on

% box on

axis equal

plot(P1.x,P1.y,P2.x,P2.y);

plot([p1x p2x],[p1y p2y],'ro-');

title('Computation of the minimum distance between two pigs P1 and P2')

hleg = legend('P1','P2','min_dist(P1,P2)');

set(hleg,'Interpreter','none');

end

Thank you.

> i have a problem with my codings.It does not seem to execute the program and it gave me an error

>

> ??? Error using ==> zeros

> Maximum variable size allowed by the program is exceeded.

>

> Error in ==> pigsfinal1 at 125

> K1 = zeros(Npts_P1-1,Npts_P2);

>

> Can someone tell me how do i solve the problem so that i can run this program.

Well, at that point in the program, what are the values of Npts_P1 and Npts_P2 ?

Tracing your code, the values should be the number of locations labeled "1"

or "2" respectively in image bb.jpeg. But we don't have your images so we can't

run the code for ourself to look at the numbers.

> % To compute the minimum distance between two polygons, we compute the

> % minimum distance between all vertices and all edges.

What is known about the polygons? If they can be triangulated into convex

regions, then you can find the minimum distances by considering the

vertices of each bounded region instead of considering the distance

between every point and every other.

> K1 = zeros(Npts_P1-1,Npts_P2);

> for i = 1:numel(P1.x)-1

Why are you being inconsistent in your usage here? In the K1 assignment

statement you called the first quantity Npts_P1 - 1, and in the for loop

you are calling that same quantity as numel(P1.x) - 1 . You are doing

unnecessary work and confusing the reader by using two representations of

the same value.

--

.signature note: I am now avoiding replying to unclear or ambiguous postings.

Please review questions before posting them. Be specific. Use examples of what you mean,

of what you don't mean. Specify boundary conditions, and data classes and value

relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

> i have a problem with my codings.It does not seem to execute the program and it gave me an error

>

> ??? Error using ==> zeros

> Maximum variable size allowed by the program is exceeded.

>

> Error in ==> pigsfinal1 at 125

> K1 = zeros(Npts_P1-1,Npts_P2);

>

> Can someone tell me how do i solve the problem so that i can run this program.

>

> this is my codings,

> .........

As far as I can make out, your algorithm looks basically correct, Bernard. However, with very large numbers of points in each polygon it looks as though you will have to forego the luxury of storing the distance between each vertex of the one polygon and each line segment of the other. You simply will not have enough memory for all such pairings. It is not a matlab "bug"; it is a limitation of your basic computer storage resources.

Based on your final output of 'min_d' (or 'min_dist' - which is it?) you apparently have no real need for such immense storage. Just store your minima and their indices as you go through the segments of each polygon and use only single dimensional arrays in the process. That will avoid the necessity of using such huge amounts of memory. What other needs do you have for saving each possible combination of vertex-to-segment distance?

Depending on the polygons, there may exist algorithms which make it possible to avoid the majority of the complex segment-to-vertex calculations with large numbers of vertices. Assuming you already know that there are distances as small as a value d, then for any given segment a rectangular box can be drawn about it having x and y coordinates a distance d beyond the segment coordinates in each direction. It can be quickly determined if any given vertex lies beyond such a box, and if so, the more complex distance calculation can be avoided. If, say, the x-coordinates of all the vertices of both polygons have a sorted version, the question of whether a vertex lies inside such a box can be even more efficiently answered.

Roger Stafford

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.

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.

Got questions?

Get answers.

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

Learn moreDiscover what MATLAB ^{®} can do for your career.

Opportunities for recent engineering grads.

Apply TodayThe 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

You can also select a location from the following list:

- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)