Info

此问题已关闭。 请重新打开它进行编辑或回答。

failed to determine the appropriate size in find

3 次查看(过去 30 天)
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

回答(3 个)

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

Jan
Jan 2012-2-27
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 2012-2-27
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 个评论
Jan
Jan 2012-2-28
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 2012-2-29
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

此问题已关闭。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by