Got Questions? Get Answers.
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:
Issue with index exceeding matrix sizes

Subject: Issue with index exceeding matrix sizes

From: Giles

Date: 18 Jul, 2011 09:51:08

Message: 1 of 2

Just wondering if anyone can see the problem with this section of code, I have been through each line seperately and pinpointed that the issue is within the for loop

Many thanks for your time

 INPUT = ones(6,6) ;
[m n] = size(INPUT) ;
SEGMENT_MAP = zeros(m,n);
segment_number = 1 ;

[idx idy] = find(INPUT==1) ;

idy(idx==1) = [] ; idx(idx==1) = [] ;
idy(idx==m) = [] ; idx(idx==m) = [] ;
idx(idy==1) = [] ; idy(idy==1) = [] ;
idx(idy==n) = [] ; idy(idy==n) = [] ;

for count=1:length(idx)
    i = idx(count) ;
    j = idy(count) ;
    b = reshape(SEGMENT_MAP(i-1:i+1,j-1:j+1),1,[])
    if (sum(b) == 0)
        a = segment_number ;
        segment_number = segment_number+1 ;
    else
        a = min(b(b>0)) ;
    end
    SEGMENT_MAP(i,j) = a;
end
SEGMENT_MAP

Subject: Issue with index exceeding matrix sizes

From: Roger Stafford

Date: 18 Jul, 2011 19:00:12

Message: 2 of 2

"Giles" wrote in message <j00vmc$61i$1@newscl01ah.mathworks.com>...
> Just wondering if anyone can see the problem with this section of code, I have been through each line seperately and pinpointed that the issue is within the for loop
> ........
 - - - - - - - - -
  As it stands your code works fine for me. Given the error message you have apparently received while in the for-loop, I would suggest you make a check as to the validity of the matlab function calls, 'sum', 'min', and 'reshape'. Perhaps somewhere on your system you have unknowingly created a variable with one of those names which is being referenced as a matrix instead of the assumed matlab function. That is something that is always to be avoided.

Roger Stafford

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