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

조회 수: 2 (최근 30일)
hothifa hasanat
hothifa hasanat 2021년 9월 26일
답변: Image Analyst 2021년 9월 26일
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

답변 (1개)

Image Analyst
Image Analyst 2021년 9월 26일
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.

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by