Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

failed to determine the appropriate size in find

1 visualización (últimos 30 días)
Octav Chipara
Octav Chipara el 27 de Feb. de 2012
Cerrada: MATLAB Answer Bot el 20 de Ag. de 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

Respuestas (3)

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

Jan
Jan el 27 de Feb. de 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 el 27 de Feb. de 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 comentarios
Jan
Jan el 28 de Feb. de 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 el 29 de Feb. de 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

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by