Thread Subject: Please take a look at this code and let me know what's wrong

Subject: Please take a look at this code and let me know what's wrong

From: Mike M

Date: 29 Oct, 2009 05:55:04

Message: 1 of 3

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.

Subject: Please take a look at this code and let me know what's wrong

From: Nasser M. Abbasi

Date: 29 Oct, 2009 06:46:57

Message: 2 of 3


"Mike M" <mccain1976@gmail.com> wrote in message
news: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.


one way, use loop

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)=n*(i-1)+j;
    end
end

(could be done witout loop if you really want to)

--Nasser

Subject: Please take a look at this code and let me know what's wrong

From: Sebastian Arslanogullari

Date: 4 Nov, 2009 20:44:03

Message: 3 of 3

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.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com