Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
creat matrix 2*2

Subject: creat matrix 2*2

From: fatima

Date: 15 Aug, 2012 20:43:17

Message: 1 of 3

Hi everyone,
would you please guide me how can i create a matrix n*n with for loop.during the programming i want to call every array of this matrix.
thank you

Subject: creat matrix 2*2

From: Nasser M. Abbasi

Date: 15 Aug, 2012 21:07:19

Message: 2 of 3

On 8/15/2012 3:43 PM, fatima wrote:
> Hi everyone,
> would you please guide me how can i create a matrix n*n with for loop.during
>the programming i want to call every array of this matrix.
> thank you
>

http://www.mathworks.com/help/techdoc/ref/zeros.html

  zeros(n) returns an n-by-n matrix of zeros. An error message appears if n is not a scalar.

Subject: creat matrix 2*2

From: dpb

Date: 15 Aug, 2012 21:29:06

Message: 3 of 3

On 8/15/2012 3:43 PM, fatima wrote:
> Hi everyone,
> would you please guide me how can i create a matrix n*n with for
> loop.during the programming i want to call every array of this matrix.
> thank you

The whole point in Matlab is to try to _avoid_ unnecessary for loops...

Creating an nxn array by accessing each element from the beginning to
cause allocation is the absolute wrong way to approach the question.

What to you want to do w/ the array when you've created it or what
should it contain on creation?

BTW, as another poster noted altho only one example was given you can
create several special matrices of size N by builtin Matlab functions--

zeros()
ones()
eye()

for example.

But, as far as a loop goes, simply referencing

x(4,4)=0;

will do end up w/ the same thing as

x=zeros(4);

or the abominable appeared to have asked for...

for i=1:4
   for j=1:4
     x(i,j)=0;
   end
end

There's another abomination in the above I've not mentioned but in
general makes the performance worse; I'll leave it as "exercise for
student" as to what that is... :)

--

Tags for this Thread

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.

Contact us