Generating a vector with a pattern?

8 vues (au cours des 30 derniers jours)
aadi
aadi le 4 Fév 2011
How do I generate the following vector efficiently for large lengths?
A=[1^1 2^2 3^3 4^4 5^5 6^6]

Réponse acceptée

Vieniava
Vieniava le 4 Fév 2011
check this out:
A=[1:6].^[1:6];
  1 commentaire
Andrew Newell
Andrew Newell le 4 Fév 2011
Stylistically better is
A = (1:6).^(1:6);
because brackets are used to construct arrays and 1:6 already is an array.

Connectez-vous pour commenter.

Plus de réponses (2)

the cyclist
the cyclist le 4 Fév 2011
>> v = 1:6;
>> A = v.^v;
Stylistically even better? ;-)
  3 commentaires
Walter Roberson
Walter Roberson le 4 Fév 2011
Now how would we do tetration, v^^v?
http://en.wikipedia.org/wiki/Tetration
Walter Roberson
Walter Roberson le 4 Fév 2011
Nevermind; 4^4^4^4 is the largest that fits within realmax.

Connectez-vous pour commenter.


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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Language Fundamentals dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by