gpt4 book ai didi

matlab - 如何矢量化这个 for loop matlab

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:42 25 4
gpt4 key购买 nike

divisible = 0;
low = input('Start Value: ');
high = input('End Value: ');
divisor = input('Divisor: ');
mask = mod([low:high],divisor);

for index = low:high
if mask(index) == 0
divisible = divisible + 1;
end
end

思路是统计没有余数的次数。

最佳答案

这是一个单行解决方案:

%#Set the inputs
LB = 3;
UB = 28;
Divisor = 3;

%#A one-line solution
Count = sum(mod((LB:UB)', Divisor) == 0);

关于matlab - 如何矢量化这个 for loop matlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13224890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com