Can anyone please help me to understand the problem here? I keep getting the error "Index exceeds matrix dimensions".

for i = 1:length(circles)
if (circles(i,1) ~= 0 & peaks(i,2) ~= 0)
xp = peaks(3,i).*cos(ang);
yp = peaks(3,i).*sin(ang);
end
end

 Accepted Answer

Replace length(circles) by size(circles,1)

2 Comments

You index circles(i,1) so i should be at most size(circles,1)
Notice though that in your "if" you index peaks(i,2) but after that you index peaks(3,i) which switches the position of the "i" .

Sign in to comment.

More Answers (2)

if size(circles,1) == size(peaks,1)
dc = cellfun(@(f)f(ang).*peaks(3,circles(:,1) & peaks(:,2)),{@cos,@sin},'un',0);
[xp,yp] = dc{:};
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!