Path: news.mathworks.com!not-for-mail
From: "Sebastian Arslanogullari" <sebastian.arslanogullari@robur.se>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Please take a look at this code and let me know what's wrong
Date: Wed, 4 Nov 2009 20:44:03 +0000 (UTC)
Organization: Robur AB
Lines: 21
Message-ID: <hcsp2j$77f$1@fred.mathworks.com>
References: <hcbano$9kc$1@fred.mathworks.com>
Reply-To: "Sebastian Arslanogullari" <sebastian.arslanogullari@robur.se>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1257367443 7407 172.30.248.35 (4 Nov 2009 20:44:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 4 Nov 2009 20:44:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 69570
Xref: news.mathworks.com comp.soft-sys.matlab:582519


Hi Mike
As Nasser wrote, there are more elegant ways to solve this without a loop.
A one-line example would be:
A = shiftdim(reshape(1:m*n,n,m),1);

Regards
Sebastian Arslanogullari

"Mike M" <mccain1976@gmail.com> wrote in message <hcbano$9kc$1@fred.mathworks.com>...
> Here's a "problem" I'm working on:
> m = input('Enter an integer m to represent the number of desired rows:')
> n = input('Enter an integer n to represent the number of desired columns:')
> A = zeros(m:n);
> for i = 1:m
>     for j = 1:n
>         A(i,j) = linspace((i-1)*n+1, (i-1)*n+j, n);
>     end
> end
> A
> It's supposed to create an m x n matrix that goes from 1:n by 1.  In other words, if you enter 3 for m and 3 for n, you should get:
> A = [1,2,3;4,5,6;7,8,9].  Entering 4 would do the same except to 16.  I'm definitely a newbie so I'm not seeing the error.