Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Find out how long after division will be an integer?

1 回表示 (過去 30 日間)
Lev Mihailov
Lev Mihailov 2020 年 7 月 28 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello! Is it possible to know when the condition will be met, what has been divided and the result is an integer?
A=[1:100];
for i=1:30
Ax(i)=A(i)/2;
if Ax(i) not a fractional number
Ax(i)=Ax(i);
else
break
end
end

回答 (2 件)

KSSV
KSSV 2020 年 7 月 28 日
if mod(x,1)==0 then x is an whole number/ integer.
  2 件のコメント
Lev Mihailov
Lev Mihailov 2020 年 7 月 29 日
I don't quite understand the given function
A(2)/2=1 % correctly and write to the array
A(3)/2= 1.5 % wrong and move on to the next correct number
A(4)/2=2 % correctly and write to the array
KSSV
KSSV 2020 年 7 月 29 日
編集済み: KSSV 2020 年 7 月 29 日
A=[1:100];
count = 0 ;
iwant = zeros([],1) ;
for i=1:30
Ax(i)=A(i)/2;
if ~mod(Ax(i),1) % Ax(i) not a fractional number
count = count+1 ;
iwant(count)=Ax(i);
end
end
iwant
You need not to use a loop.
idx = mod(Ax,1) ;
iwant = Ax(idx==0) ;

madhan ravi
madhan ravi 2020 年 7 月 28 日
Simple use mod()
doc mod

製品

Community Treasure Hunt

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

Start Hunting!

Translated by