find non zero elements in array

im trying to implement find non zero elements without using "find' toolbox in simulink. im using matlab function block as shown on the screenshot
function y = non_zero(u)
x=find(u);
y=x

Antworten (1)

Soniya Jain
Soniya Jain am 10 Jul. 2021

0 Stimmen

As mentioned in the error, that y is non-tunable, so have you tried to select the 'Variable Size' checkbox by specifying the upper bounds in the Size box?
Also, find function will return the index of the non zero elements in a vector, so if you want values at the index, you can code in the following way:
function y = non_zero(u)
x = find(u); % will return index of non-zero elements
y = y(x);
end

3 Kommentare

Le Dyk Zuy
Le Dyk Zuy am 10 Jul. 2021
i need the index of the non zero elements in a vector, not the actual value
Soniya Jain
Soniya Jain am 11 Jul. 2021
So have you tried to select the 'Variable Size' checkbox by specifying the upper bounds in the Size box?
Le Dyk Zuy
Le Dyk Zuy am 12 Jul. 2021
i did, now it says:
Output 'y' has variable size but the upper bound is not specified; explicit upper bound must be provided.
Error in port widths or dimensions. 'Output Port 1' of 'hdl_code_function_test/MATLAB Function/u_port' is a one dimensional vector with 1 elements.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Code Generation finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 9 Jul. 2021

Kommentiert:

am 12 Jul. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by