Curious
Posts:
2,050
Registered:
12/6/04


Re: How to create a matrix with increasing power?
Posted:
Feb 22, 2014 12:35 PM


"John D'Errico" <woodchips@rochester.rr.com> wrote in message <le8oju$s2t$1@newscl01ah.mathworks.com>... > "someone" wrote in message <le8iet$dv5$1@newscl01ah.mathworks.com>... > > "Sven " <svendotholcombe@gmaildot.com> wrote in message <le8g3t$7kg$1@newscl01ah.mathworks.com>... > > > "Timothy Liang" <timothy.tliang@gmail.com> wrote in message <le8e48$1uu$1@newscl01ah.mathworks.com>... > > > > I just started to learn how to use matlab... Just a simply question... > > > > > > > > Is there a simple way create a matrix like this? > > > > x= 1, 2, 3, 4, 5 > > > > > > > > matrix X=1^0 1^1 1^2 1^3 > > > > 2^0 2^1 2^2 2^3 > > > > 3... > > > > 4... > > > > 5... > > > > > > > > Thanks!! > > > > > > Yep, try this: > > > > > > bsxfun(@power, 0:5, (1:5)') > > > ans = > > > > > > 0 1 2 3 4 5 > > > 0 1 4 9 16 25 > > > 0 1 8 27 64 125 > > > 0 1 16 81 256 625 > > > 0 1 32 243 1024 3125 > > > > I don't think this is what Timothy asked for. > > > > If I understand the OP he wants a 4X5 matrix where given: > > > > x = 1:5 > > y = 10^(0:3) > > > > for ii = 1:length(x) > > X(:,ii) = x(ii)*y; > > end > > > > I don't have MATLAB installed on this computer, > > but the result should be: > > X= [ > > 1 1 2 3 > > 1 2 4 8 > > 1 3 9 27 > > 1 4 16 64 > > 1 5 25 125] > > > > which is what I believe the OP asked for. (Maybe its transposed.) > > > > I know there are more MATLAB efficient ways, > > but Timothy did ask for a SIMPLE way. > > Geez. BSXFUN IS the simple way. > > Admittedly, the answer given that used bsxfun was > wrong in the respect that the poster wanted a > slightly different result. > > But no, the poster did explicitly ask for a 5x4 matrix, > NOT a 4x5 matrix. > > bsxfun(@power, (1:5)',0:3) > ans = > 1 1 1 1 > 1 2 4 8 > 1 3 9 27 > 1 4 16 64 > 1 5 25 125 > > As far as the simple way, I find it difficult to understand > how a trivial (and clear) one line solution is not the > simpler solution than something that uses loops and > indexing. > > Finally if you don't understand how to use bsxfun, then > it is high time to learn, as this is a powerful tool in > MATLAB. > > John
John,
Like I said in my original post, I knew there was a more efficient way but I didn't have MATLAB installed on the computer I was on at the time. My main concern was to alert the OP that the solution proposed was not what he asked for. Maybe, in hindsight, I shouldn't have emphasized "simple" in my post.

