BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / matlab / #3075同步于 2008/4/1
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Matlab机器人发帖

【求助】怎么求满足M<2^n的最小整数n

coolbin
2008/4/1镜像同步5 回复
其中M是给定的正整数 有没有比较简便的办法求n?
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
iamsai机器人#1 · 2008/4/1
n = ceil(log2(M)) + ( ceil(log2(M)) == log2(M) ); 【 在 coolbin 的大作中提到: 】 : 其中M是给定的正整数 : 有没有比较简便的办法求n?
QuestionMark机器人#2 · 2008/4/1
看到这个忽然萌生一个问题(与lz问题无关^^) temp = log2(M); n = ceil(temp) + ( ceil(temp) == temp ); 这样会不会运行的更快? 谁用matlab跑一下profile看看吧 【 在 iamsai 的大作中提到: 】 : n = ceil(log2(M)) + ( ceil(log2(M)) == log2(M) );
iamsai机器人#3 · 2008/4/1
你这个做法确实快,真是受教了o(∩_∩)o 循环次数1e7: 1.不设temp,2.914s; 2.按你的设法,2.027s; 3.若temp = ceil(log2(M)), 则是1.993s 【 在 QuestionMark 的大作中提到: 】 : 看到这个忽然萌生一个问题(与lz问题无关^^) : temp = log2(M); : n = ceil(temp) + ( ceil(temp) == temp ); : ...................
coolbin机器人#4 · 2008/4/1
Thank you!
cofreely机器人#5 · 2008/4/4
p = nextpow2(A) returns the smallest power of two that is greater than or equal to the absolute value of A. (That is, p that satisfies 2^p >= abs(A)).