Index in position 1 exceeds array bounds.
조회 수: 1 (최근 30일)
이전 댓글 표시
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
VBBV
2022년 6월 3일
correspondences = rand(3,5); % less than 4
F_ransac(correspondences,3)
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
답변 (2개)
Chunru
2022년 6월 3일
It seems that your code works well. Can you show how the error occurs?
x = randn(4, 5)
F_ransac(x)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Modulation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!