In this code I have a result but I want it as integer numbers.

2 visualizzazioni (ultimi 30 giorni)
n = 12;
m = 6;
numbers = linspace(1, n, m)
index = 1;
theSums = [];
for k = 1 : length(numbers)
for k2 = 1 : length(numbers)
num1(index) = numbers(k);
num2(index) = numbers(k2);
theSums(index) = num1(index) + num2(index);
fprintf('%.4f + %.4f = %.4f\n', num1(index), num2(index), theSums(index));
index = index + 1;
end
end
[uniqueSums, indexes] = unique(theSums)
% Extract only the unique numbers:
num1 = num1(indexes)
num2 = num2(indexes)
% Double check.
for k = 1 : length(indexes)
fprintf('%.4f + %.4f = %.4f\n', num1(k), num2(k), uniqueSums(k));
end

Risposte (1)

Image Analyst
Image Analyst il 26 Set 2021
round() will turn floating point numbers (doubles) with fractions into floating point numbers (doubles) with no fraction (the fraction is 0).
Or you can use int32() to cast your variable into a 32 bit integer, which of course also does the rounding.

Categorie

Scopri di più su Operators and Elementary Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by