Generating a vector with a pattern?

조회 수: 17 (최근 30일)
aadi
aadi 2011년 2월 4일
How do I generate the following vector efficiently for large lengths?
A=[1^1 2^2 3^3 4^4 5^5 6^6]

채택된 답변

Vieniava
Vieniava 2011년 2월 4일
check this out:
A=[1:6].^[1:6];
  댓글 수: 1
Andrew Newell
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
the cyclist 2011년 2월 4일
>> v = 1:6;
>> A = v.^v;
Stylistically even better? ;-)
  댓글 수: 3
Walter Roberson
Walter Roberson 2011년 2월 4일
Now how would we do tetration, v^^v?
http://en.wikipedia.org/wiki/Tetration
Walter Roberson
Walter Roberson 2011년 2월 4일
Nevermind; 4^4^4^4 is the largest that fits within realmax.

댓글을 달려면 로그인하십시오.


Jan
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
Matt Fig
Matt Fig 2011년 2월 4일
How about:
v = 1:min(n,143);
x = inf(1,n);
x(v) = v.^v;
Jan
Jan 2011년 2월 4일
@Matt: Some percent faster. I insert it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by