【问题标题】:finding all factors of a number [closed]找到一个数字的所有因数[关闭]
【发布时间】:2014-05-14 11:17:04
【问题描述】:

我是 Matlab 新手,对于 a、l 和 w 的值,我需要找到数据集中 l 的所有值以及对应的 w 值。

a=10;
l=(0:10)
w=(0:10)
for l,d
       if a == l.*w
           disp(l) 
           disp(w)
       end
end

【问题讨论】:

  • 您的问题不清楚。你能详细说明一下吗?
  • 请@user3636220,尽量更清楚地解释您需要帮助的内容。
  • 在不了解您的实际问题的情况下,标题暗示this question 可能会有所帮助。

标签: matlab loops if-statement disp


【解决方案1】:

不确定你想做什么,但我认为你的代码可以如下:

a = 10;
l = 0:a; %// actually, it would suffice to check numbers up to floor(a/2)
ind = rem(a,l)==0; %// logical index that tells if remainder is zero or not
disp([l(ind); a./l(ind)])

结果:

     1     2     5    10
    10     5     2     1

您可以更直接地使用 Matlab 的 factor 函数:

f = factor(a);
disp([f; a./f])

结果:

     2     5
     5     2

【讨论】:

  • factor 函数似乎是要走的路。如果只对一组受限制的值感兴趣,则可以随后减少输出。检查help union
猜你喜欢
  • 2018-09-04
  • 1970-01-01
  • 2016-03-17
  • 2014-05-18
  • 1970-01-01
  • 2011-08-13
  • 2013-08-14
  • 2015-11-03
  • 2012-11-06
相关资源
最近更新 更多