Problem with for (nested) loops

조회 수: 1 (최근 30일)
Abhivyakti
Abhivyakti 2013년 7월 3일
Hi
I am making a function. This is part of it. Here, I have a 1 x 256 array and i need to find out a corresponding value for each of its elements (256 values).
If i enter the set of commands (below) to my data individually, i get precise values of each of 256 elements of the array. But if I use it as a function, I only get values corresponding to the first and the last elements of the array, while the rest are all 0.
So basically there is some error with the loops that i've formed here.
  • The first loop signifies a summation from 1st to 'b'th element, i.e the element under consideration.
  • The second loop also signifies a summation from 'b'th element to the 256th element.
  • Finally, each value ('e') is stored in the matrix j.
Any help would be great. Am stuck on this from the past 2 days.
CODE:
for b=1:256
for c=1:b
w=l(1,c);
f=0;
f= (f + ( w* (log (w))));
end
y=-(f);
for d=b:256
v=l(1,d);
g=0;
g= (g + ( v* (log (v))));
end
u=-(g);
i=normpdf(y,0,1);
j=normpdf(u,0,1);
e= -(log(i))-(log(j))-(y/i)-(u/j);
j(1,b)=e;
end
val=j;

채택된 답변

the cyclist
the cyclist 2013년 7월 3일
I think the main problem with your code (but there may be others) is that when you call the line
j = normpdf(u,0,1);
you are unintentionally (I assume) overwriting your vector j(1,b), so j(1,b) is written from scratch every time. You need different variable names there.
  댓글 수: 1
Abhivyakti
Abhivyakti 2013년 7월 8일
Thankyou so much! :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by