Size mismatch error when trying to find a row in an array that is the same as a variable vector.

조회 수: 2 (최근 30일)
idx = find(all(waypoints == target, 2), 1);
So this is a line of code in a function of a simulink function block.
The array waypoints is just an array with two columns and any amount of rows.
I'm trying to find the index at which the [2 x 1] vector target is equal to a row within the waypoints array.
But when the simulation is run, it tells me there is a size mismatch where I first try to define idx. Surely they don't need to match as the purpose is just to find where one of waypoints rows are equal to target?
function targetnew = setwaypoint(d2w,target,accuracy)
waypoints = [100 150; 200 400; 175 275; 600 300]; % pre-defined waypoints (in the future imported as a large array from an external source.
if isempty(target)
target = waypoints(1,:); % initializes the first waypoint.
end
idx = find(all(waypoints == target, 2), 1); % find the first row that match
if isempty(idx) % no row found!
error('could not find target in the waypoints');
end
if d2w > accuracy
idx = idx + 1; % if target waypoint is reached, add one to index.
end
if idx > size(waypoints, 1) % attempt to avoid any issues if simulation time is greater than time taken to reach final waypoint.
targetnew = waypoints(end, :);
else
targetnew = waypoints(idx, :);
end
end
  댓글 수: 2
Sam Elliott
Sam Elliott 2019년 3월 10일
I never knew about this, but using ismember with 'rows' worked, thank you. I'm still having issues but they're unrelated to this specific issue now so I'll post it in a new question.

댓글을 달려면 로그인하십시오.

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2019년 3월 8일
The problem is here.
If target is 1x2 and waypoints is nx2, you can't do waypoints==target
waypoints = [100 150; 200 400; 175 275; 600 300];
target=[]
if isempty(target)
target = waypoints(1,:); % initializes the first waypoint.
end
waypoints==target
  댓글 수: 4
Fangjun Jiang
Fangjun Jiang 2019년 3월 11일
If you are using R2018b as indicated, you should have no error running this:
waypoints = [100 150; 200 400; 175 275; 600 300];
target=[200 400];
idx = find(~all(waypoints-target, 2), 1)
idx =
2

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by