In this code I have a result but I want it as integer numbers.
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Image Analyst
on 26 Sep 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.
0 Comments
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!