Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

failed to determine the appropriate size in find

3 Ansichten (letzte 30 Tage)
Octav Chipara
Octav Chipara am 27 Feb. 2012
Geschlossen: MATLAB Answer Bot am 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

Antworten (3)

Grzegorz Knor
Grzegorz Knor am 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 am 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 am 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 Kommentare
Jan
Jan am 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 am 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

Diese Frage ist geschlossen.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by