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:
a matrix question

Subject: a matrix question

From: Konst

Date: 8 Feb, 2011 20:43:03

Message: 1 of 8

I have this part of algorithm:
s=0;
for j=1:100
 for N=100:100:1000
     for i=1:N
nk=rand(1000,1);
d=-100*log(nk);
Sn=sum(d);
s=s+(i/N)*(d(i)/Sn);
P(j)=-s*s;
end
s=0
end

That is,for every N I calculate a P 100 times. Are these commands right for what I'm trying to do? Because it give's me 100 P instead of 1000 and I can't figure out why!And if it is, how can I put my results to a matrix like this:

100 firstP secondP thirdP ... jthP
200 firstP SecondP thirdP ... jthP
.
.
.
1000 firstP....

You better view the original format

Subject: a matrix question

From: ImageAnalyst

Date: 8 Feb, 2011 20:47:02

Message: 2 of 8

I see three "for"s but only two "end"s so I don't know which loop
stops where. It looks like P is inside either the "i" loop or the "N"
loop. Try again.

Subject: a matrix question

From: Konst

Date: 8 Feb, 2011 21:46:03

Message: 3 of 8

s=0;
for j=1:100
   for N=100:100:1000
       for i=1:N
nk=rand(1000,1);
d=-100*log(nk);
Sn=sum(d);
s=s+(i/N)*(d(i)/Sn);
P(j)=-s*s;
       end
s=0
  end
end

This is the right one!

Subject: a matrix question

From: TideMan

Date: 8 Feb, 2011 22:01:16

Message: 4 of 8

On Feb 9, 10:46 am, "Konst " <konstan...@hotmail.com> wrote:
> s=0;
> for j=1:100
>    for N=100:100:1000
>        for i=1:N
> nk=rand(1000,1);
> d=-100*log(nk);
> Sn=sum(d);
> s=s+(i/N)*(d(i)/Sn);
> P(j)=-s*s;
>        end
> s=0
>   end
> end
>
> This is the right one!

You need to have an index for N in P.
P(round(N/100),j)=-s*s;

Here is some gratuitous advice:
1. Don't use i and j for indices. It overwrites their default values
of sqrt(-1). Use something like jrun for j and iN for i.
2. Preallocate P before starting the for loops, P=zeros(10,100). It
will speed up the computations.
3. Eliminate the inner for loop by using matrices instead of
vectors. nk=rand(1000,N) and so on.

Subject: a matrix question

From: Konst

Date: 9 Feb, 2011 17:24:05

Message: 5 of 8

Ok, so I followed your instructions and works perfectly!Thank you sooo much!! But now i have another problem. I calculate the standard deviation and mean for each column and plot the confidence intervals like this:
m=mean(P);
s=std(P);
plot(mk)
hold on
plot(m-s)
hold on
plot(m+s)
 and while in the y-axis everything is fine, the x-axes has values from 0 to 10 while i want it to have the N values (100-1000).
When I try to do it manually using the
axis([xmin xmax ymin ymax])
function, it gathers all the data in the y-axis so the rest of the graph is blank. I probably have a mistake in my chain of commands, and somehow I need to connect N with m,s but I don't know how. I thought to put the above calculations in the loop but it doesn't work. Any ideas??And thanks again for your help and time.

Subject: a matrix question

From: Steven_Lord

Date: 9 Feb, 2011 20:18:43

Message: 6 of 8



"Konst " <konstance1@hotmail.com> wrote in message
news:iiuijk$koi$1@fred.mathworks.com...
> Ok, so I followed your instructions and works perfectly!Thank you sooo
> much!! But now i have another problem. I calculate the standard deviation
> and mean for each column and plot the confidence intervals like this:
> m=mean(P);
> s=std(P);
> plot(mk)
> hold on
> plot(m-s)
> hold on
> plot(m+s)
> and while in the y-axis everything is fine, the x-axes has values from 0
> to 10 while i want it to have the N values (100-1000).

Look at the help for PLOT. What does it say it does when you call it with
one input?

Call it with two inputs corresponding to the X and Y coordinates of your
data. You already have your Y data; you just need to build the right X
data.

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

Subject: a matrix question

From: Konst

Date: 9 Feb, 2011 22:07:04

Message: 7 of 8

I tried to fix that but I think that the axis is not the problem.The problem is in the
P(j,round(N/100))=-s*s; command which divides N/100 so automatically instead of having N=1000 I've got 1000/100=10..Am I correct??How can I bypass this?Maybe there is a different function tha does what I wanted in my initial message for the matrix?

Subject: a matrix question

From: Konst

Date: 10 Feb, 2011 14:16:03

Message: 8 of 8

"Konst " <konstance1@hotmail.com> wrote in message <iiv368$95t$1@fred.mathworks.com>...
> I tried to fix that but I think that the axis is not the problem.The problem is in the
> P(j,round(N/100))=-s*s; command which divides N/100 so automatically instead of having N=1000 I've got 1000/100=10..Am I correct??How can I bypass this?Maybe there is a different function tha does what I wanted in my initial message for the matrix?

I also tried P(j,round(N/10))=-s*s which destroys the shape of my curve completely. I tried it also with N=100:10:1000 but it does the same. The only thing that seems to work somehow is N=100:1000 along with P(j,round(N/1.0))=-s*s but except for the part that it took the program 45 minutes to run, it 'curls' the lines of the curve(but it keeps its general shape) and give me major deviation from the data.
Any other suggestions anyone?

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