Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

failed to determine the appropriate size in find

1 view (last 30 days)
Octav Chipara
Octav Chipara on 27 Feb 2012
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi,
I'm having some problem getting matlab coder to identify a variable used as result from find as a scalar. Below is a simple testcase:
function [ y ] = a_test()
x = zeros(5, 1);
k1 = find(x > 1, 1);
if (isempty(k1))
k1 = 0;
end
y = x(1:k1);
end
The coder fails to generate code due in the last line because it cannot infer that k1 is a scalar. Is there a way of handling this?
Thanks, -- Octav

Answers (3)

Grzegorz Knor
Grzegorz Knor on 27 Feb 2012
What should be the y?
Maybe replace:
if (isempty(k1))
k1 = 0;
end
with:
if (isempty(k1))
k1 = 1;
end

Jan
Jan on 27 Feb 2012
While in you code k1 is a scalar, 1:k1 is not when k1 equals 0.
function y = a_test()
x = zeros(5, 1);
k1 = find(x > 1, 1);
if isempty(k1)
y = ???;
else
y = x(1:k1);
end

Fred Smith
Fred Smith on 27 Feb 2012
Try this:
function [ y ] = a_test()
x = zeros(5, 1);
k1 = find(x > 1, 1);
if (isempty(k1))
k1 = 0;
end
y = x(1:k1(1)); % Added k1(1)
end
HTH,
Fred
  2 Comments
Jan
Jan on 28 Feb 2012
After the IF-block, k1 is always a scalar. Therefor k1(1) is not useful. In addition y=x(1:k1(1)) still fails, if k1 is 0, because 1:0 is empty.
Fred Smith
Fred Smith on 29 Feb 2012
k1 is always scalar but MATLAB Coder does not know that. It does know that k1(1) is always a scalar. x(1:0) is legal and returns an empty matrix. I don't know what the intended behavior is in this case but the changed code matches the behavior of Octav's original code.
-Fred

This question is closed.

Products

Community Treasure Hunt

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

Start Hunting!