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:
how to enter conditional matrix

Subject: how to enter conditional matrix

From: Kennedy Kwasi

Date: 30 Mar, 2012 12:59:12

Message: 1 of 3

How do i write a matlab script to generate an 80x80 matrix to satisfy the following conditions:
for i=1:80; for j=1:80 , if j==i , a(i,j)=2*i
else if j=i+2, for i=1:78 or j=i-2, for i=3:80, a(i,j)=0.5*i
else if j=i+4, for i=1:76 or j=i-2, for i=5:80, a(i,j)=0.25*i
else a(i,j)=0

Subject: how to enter conditional matrix

From: Steven_Lord

Date: 30 Mar, 2012 13:19:09

Message: 2 of 3



"Kennedy Kwasi" <y2k394@gmail.com> wrote in message
news:jl4amv$2ng$1@newscl01ah.mathworks.com...
> How do i write a matlab script to generate an 80x80 matrix to satisfy the
> following conditions:
> for i=1:80; for j=1:80 , if j==i , a(i,j)=2*i
> else if j=i+2, for i=1:78 or j=i-2, for i=3:80, a(i,j)=0.5*i
> else if j=i+4, for i=1:76 or j=i-2, for i=5:80, a(i,j)=0.25*i
> else a(i,j)=0

No idea, because it looks like you're trying to nest a FOR loop over i
inside another FOR loop also over i.

If I had to guess I'd say the function you're looking for is DIAG, but if
you could show the group a small example (say 8-by-8) of the matrix you want
to result from running the code, the group may be able to offer more
specific help.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: how to enter conditional matrix

From: Roger Stafford

Date: 30 Mar, 2012 19:00:15

Message: 3 of 3

"Kennedy Kwasi" wrote in message <jl4amv$2ng$1@newscl01ah.mathworks.com>...
> How do i write a matlab script to generate an 80x80 matrix to satisfy the following conditions:
> for i=1:80; for j=1:80 , if j==i , a(i,j)=2*i
> else if j=i+2, for i=1:78 or j=i-2, for i=3:80, a(i,j)=0.5*i
> else if j=i+4, for i=1:76 or j=i-2, for i=5:80, a(i,j)=0.25*i
> else a(i,j)=0
- - - - - - - - - - - -
  You've committed horrible atrocities with matlab's syntax, but I'm guessing you mean this:

 n = 80;
 M = diag((1:n)*2)+diag((1:n-2)/2,2)+diag((3:n)/2,-2) ...
          +diag((1:n-4)/4,4)+diag((5:n)/4,-4);

(I'm assuming you mean j=i-4 instead of j=i-2 in the line

 "else if j=i+4, for i=1:76 or j=i-2, for i=5:80, a(i,j)=0.25*i" .)

Roger Stafford

Tags for this Thread

No tags are associated with 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