Convert integer value to a vector

31 Ansichten (letzte 30 Tage)
Adam s
Adam s am 8 Sep. 2022
Kommentiert: Walter Roberson am 8 Sep. 2022
I would like to convert the value 1101 to a 1x4 [1,1,0,1] in simulink

Antworten (1)

Moe_2015
Moe_2015 am 8 Sep. 2022
You can do this by placing a matlab function block in Simulink. Your function should look like below. Connect your input signal (that has 1101) and the output will be a vector [1,1,0,1]
function vector = n_to_vector(n)
vector = num2str(n) - '0';
end
  5 Kommentare
Walter Roberson
Walter Roberson am 8 Sep. 2022
assert(n >0 && n <= intmax('uint64'));
longest_possible_integer = 20;
charvec = blanks(longest_possible_integer);
vector = zeros(1, longest_possible_integer);
coder.varsize('charvec', 'vector', [1 longest_possible_integer]);
charvec = sprintf('%u', n);
vector = charvec - '0';
Here, longest_possible_integer = length(sprintf('%u', intmax('uint64'))) . Any input that is large magnitude than that will get converted in scientific notation by sprintf() and the process will fail

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by