Generating a vector with a pattern?
1 回表示 (過去 30 日間)
古いコメントを表示
How do I generate the following vector efficiently for large lengths?
A=[1^1 2^2 3^3 4^4 5^5 6^6]
0 件のコメント
採用された回答
Vieniava
2011 年 2 月 4 日
check this out:
A=[1:6].^[1:6];
1 件のコメント
Andrew Newell
2011 年 2 月 4 日
Stylistically better is
A = (1:6).^(1:6);
because brackets are used to construct arrays and 1:6 already is an array.
その他の回答 (2 件)
the cyclist
2011 年 2 月 4 日
>> v = 1:6;
>> A = v.^v;
Stylistically even better? ;-)
3 件のコメント
Walter Roberson
2011 年 2 月 4 日
Now how would we do tetration, v^^v?
http://en.wikipedia.org/wiki/Tetration
Jan
2011 年 2 月 4 日
This job overflows fast for large lengths and computing 200^200 is a waste of time.
EDITED: With Matt Fig's further improvements:
function x = XpowerX(n)
v = 1:min(n,143);
x = inf(1,n);
x(v) = v.^v;
This much faster than the direct 1:n approach:
tic; for i = 1:15000; y = XpowerX(i); end; toc
% 1.8 sec
tic; for i = 1:15000; v = 1:i; y = v.^v; end; toc
% 60 sec
2 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!