Index in position 1 exceeds array bounds.

2 views (last 30 days)
Matis Tartie
Matis Tartie on 3 Jun 2022
Answered: Matis Tartie on 3 Jun 2022
As a task I have to implement a RANSAC algorithm. There is an array called correspondences which is 4xn, where the first 2 rows are x1 and y1 and the last two rows are x2 and y2. These are coordinates for pixels. The task here is to seperate the coordinates into their own homogenious variables. Before that one must provide some optional variables for the function. I used an Input Parser for that and it seems to work. This is what I did:
function [epsilon, p, tolerance, x1_pixel, x2_pixel] = F_ransac(correspondences, varargin)
% This function implements the RANSAC algorithm to determine
% robust corresponding image points
p1 = inputParser;
addOptional(p1, 'epsilon', 0.5, @(x)validateattributes(x, {'numeric'}, {'<',1}, {'>',0}));
addOptional(p1, 'p', 0.5, @(x)validateattributes(x, {'numeric'}, {'<',1}, {'>',0}));
addOptional(p1, 'tolerance', 0.01, @(x)validateattributes(x, {'numeric'}));
parse(p1);
epsilon = p1.Results.epsilon;
p = p1.Results.p;
tolerance = p1.Results.tolerance;
x1_pixel = ones(3,size(correspondences,2));
x1_pixel(1:2,:) = correspondences(1:2,:);
x2_pixel = ones(3,size(correspondences,2));
x2_pixel(1:2,:) = correspondences(3:4,:);
end
This is constantly giving me the error:
Index in position 1 exceeds array bounds.
Error in F_ransac (line 16)
x1_pixel(1:2,:) = correspondences(1:2,:);
Why is it giving me this error? What could I do to avoid it?
  1 Comment
VBBV
VBBV on 3 Jun 2022
correspondences = rand(3,5); % less than 4
F_ransac(correspondences,3)
Index in position 1 exceeds array bounds. Index must not exceed 3.

Error in solution>F_ransac (line 19)
x2_pixel(1:2,:) = correspondences(3:4,:);
function [epsilon, p, tolerance, x1_pixel, x2_pixel] = F_ransac(correspondences, varargin)
% This function implements the RANSAC algorithm to determine
% robust corresponding image points
p1 = inputParser;
addOptional(p1, 'epsilon', 0.5, @(x)validateattributes(x, {'numeric'}, {'<',1}, {'>',0}));
addOptional(p1, 'p', 0.5, @(x)validateattributes(x, {'numeric'}, {'<',1}, {'>',0}));
addOptional(p1, 'tolerance', 0.01, @(x)validateattributes(x, {'numeric'}));
parse(p1);
epsilon = p1.Results.epsilon;
p = p1.Results.p;
tolerance = p1.Results.tolerance;
x1_pixel = ones(3,size(correspondences,2));
x1_pixel(1:2,:) = correspondences(1:2,:);
x2_pixel = ones(3,size(correspondences,2));
x2_pixel(1:2,:) = correspondences(3:4,:);
end
May be you are calling function with input arguments of incorrect size

Sign in to comment.

Answers (2)

Chunru
Chunru on 3 Jun 2022
It seems that your code works well. Can you show how the error occurs?
x = randn(4, 5)
x = 4×5
1.7007 1.0577 1.2506 0.4107 1.0085 -0.3642 0.0145 0.4001 -0.3132 0.0327 0.0062 -0.1212 0.1764 -1.7733 -1.0230 -0.3822 -0.4836 0.4640 -0.9320 -1.5065
F_ransac(x)
ans = 0.5000
function [epsilon, p, tolerance, x1_pixel, x2_pixel] = F_ransac(correspondences, varargin)
% This function implements the RANSAC algorithm to determine
% robust corresponding image points
p1 = inputParser;
addOptional(p1, 'epsilon', 0.5, @(x)validateattributes(x, {'numeric'}, {'<',1}, {'>',0}));
addOptional(p1, 'p', 0.5, @(x)validateattributes(x, {'numeric'}, {'<',1}, {'>',0}));
addOptional(p1, 'tolerance', 0.01, @(x)validateattributes(x, {'numeric'}));
parse(p1);
epsilon = p1.Results.epsilon;
p = p1.Results.p;
tolerance = p1.Results.tolerance;
x1_pixel = ones(3,size(correspondences,2));
x1_pixel(1:2,:) = correspondences(1:2,:);
x2_pixel = ones(3,size(correspondences,2));
x2_pixel(1:2,:) = correspondences(3:4,:);
end

Matis Tartie
Matis Tartie on 3 Jun 2022
Yes I see. I just noticed there is something wrong with the correspondences array. Thank you for your time

Tags

Community Treasure Hunt

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

Start Hunting!