Eval, sum and two loops!
이전 댓글 표시
I have a following code:
Zhat = zeros(length(MIS),1);
for i = 1:length(MIS);
xfirst1 = MIS(i,1);
xfirst2 = MIS(i,2);
Zhat(i) = Theta(1,1) + eval(BFMS1)*Theta(1,2) + eval(BFMS2)*Theta(1,3) + eval(BFMS3)*Theta(1,4) + eval(BFMS4)*Theta(1,5) + ...
eval(BFMS5)* Theta(1,6) + eval(BFMS6)*Theta(1,7) + eval(BFMS7)*Theta(1,8) + eval(BFMS8)*Theta(1,9) + eval(BFMS9)*Theta(1,10) + ...
eval(BFMS10)*Theta(1,11) + eval(BFMS11)*Theta(1,12) + eval(BFMS12)*Theta(1,13) + eval(BFMS13)*Theta(1,14) + eval(BFMS14)*Theta(1,15) + ...
eval(BFMS15)*Theta(1,16) + eval(BFMS16)*Theta(1,17) + eval(BFMS17)*Theta(1,18) + eval(BFMS18)*Theta(1,19) + eval(BFMS19)*Theta(1,20) + ...
eval(BFMS20)*Theta(1,21) + eval(BFMS21)*Theta(1,22) + eval(BFMS22)*Theta(1,23) + eval(BFMS23)*Theta(1,24) + eval(BFMS24)*Theta(1,25) + ...
eval(BFMS25)*Theta(1,26) + eval(BFMS26)*Theta(1,27) + eval(BFMS27)*Theta(1,28) + eval(BFMS28)*Theta(1,29) + eval(BFMS29)*Theta(1,30) + ...
eval(BFMS30)*Theta(1,31) + eval(BFMS31)*Theta(1,32) + eval(BFMS32)*Theta(1,33) + eval(BFMS33)*Theta(1,34) + eval(BFMS34)*Theta(1,35) + ...
eval(BFMS35)*Theta(1,36) + eval(BFMS36)*Theta(1,37) + eval(BFMS37)*Theta(1,38) + eval(BFMS38)*Theta(1,39) + eval(BFMS39)*Theta(1,40) + ...
eval(BFMS40)*Theta(1,41) + eval(BFMS41)*Theta(1,42) + eval(BFMS42)*Theta(1,43) + eval(BFMS43)*Theta(1,44) + eval(BFMS44)*Theta(1,45) + ...
eval(BFMS45)*Theta(1,46) + eval(BFMS46)*Theta(1,47) + eval(BFMS47)*Theta(1,48) + eval(BFMS48)*Theta(1,49) + eval(BFMS49)*Theta(1,50) + ...
eval(BFMS50)*Theta(1,51) + eval(BFMS51)*Theta(1,52) + eval(BFMS52)*Theta(1,53) + eval(BFMS53)*Theta(1,54) + eval(BFMS54)*Theta(1,55) + ...
eval(BFMS55)*Theta(1,56) + eval(BFMS56)*Theta(1,57) + eval(BFMS57)*Theta(1,58) + eval(BFMS58)*Theta(1,59) + eval(BFMS59)*Theta(1,60) + ...
eval(BFMS60)*Theta(1,61) + eval(BFMS61)*Theta(1,62) + eval(BFMS62)*Theta(1,63) + eval(BFMS63)*Theta(1,64) + eval(BFMS64)*Theta(1,65) + ...
eval(BFMS65)*Theta(1,66) + eval(BFMS66)*Theta(1,67) + eval(BFMS67)*Theta(1,68) + eval(BFMS68)*Theta(1,69) + eval(BFMS69)*Theta(1,70) + ...
eval(BFMS70)*Theta(1,71) + eval(BFMS71)*Theta(1,72) + eval(BFMS72)*Theta(1,73) + eval(BFMS73)*Theta(1,74) + eval(BFMS74)*Theta(1,75) + ...
eval(BFMS75)*Theta(1,76) + eval(BFMS76)*Theta(1,77) + eval(BFMS77)*Theta(1,78) + eval(BFMS78)*Theta(1,79) + eval(BFMS79)*Theta(1,80);
end
The problem is Zhat(i) part. I try to create a loop but I couldn't do it. Can anybody help me to reduce manuel effort? BFMS are like that: BFMS40= max(0, xfirst1 - 97)*max(0, xfirst2 - 114) Theta is a matrix 1x80 double; so we have a 79 BFMS. MIS is another matrix MIS(1,1)=58 140 which indicates xfirst1 and xfirst2
Thanks!
댓글 수: 1
"The problem is Zhat(i) part"
Yes, it is.
For many reasons. Pointless copy-and-paste is the first reason: did you know that computers are basically good at doing one thing: repeating simple operations millions of times a second. So when you sit for ten minutes and copy-and-paste code like that you are basically doing the computers job for it. Why waste your life repeatedly copying and altering some simple code when your computer is designed to do exactly that?
Any time you think to yourself "I will just copy this fifty times..." then you need to think about your concept.
The next major issue is the idea that you should be accessing lots of variables using eval. Why do this? Read this to start to learn why accessing variables like that is a bad way to write code:
"Can anybody help me to reduce manuel effort?"
Yes, but you will have to actually tell use what you are trying to do, rather than simply showing us some buggy code. If you explain your task, then we can show you an efficient way to solve it.
For a start: how do you get all of those variables into the workspace? If you use load, then if you load into a structure you can avoid this whole ugly problem.
답변 (1개)
Star Strider
2017년 2월 5일
편집: Star Strider
2017년 2월 5일
I assume ‘BFMS’ are individual variables created in another program.
I would do the conversion once, put the ‘BFMS’ data in an array, and then just index everything.
One of these examples should work for you:
for k1 = 1:79
BFMS(k1) = eval(sprintf('BFMS%d', k1)); % Vector
end
for k1 = 1:79
BFMS(k1,:) = eval(sprintf('BFMS%d', k1)); % Matrix By Rows
end
for k1 = 1:79
BFMS(:,k1) = eval(sprintf('BFMS%d', k1)); % Matrix By Columns
end
for k1 = 1:79
BFMS{k1} = eval(sprintf('BFMS%d', k1)); % Cell Array
end
NOTE — This is UNTESTED CODE. It should work.
댓글 수: 5
Dorothy
2017년 2월 5일
Star Strider
2017년 2월 5일
My pleasure.
You don’t need to use eval after you use one of the loop options I described. I didn’t know ‘BFMS’ was symbolic, but it doesn’t matter because the eval call converts it to a double.
See if this slightly-tweaked version of your loop does what you want:
Zhat=zeros(length(MIS),1);
Zhat=zeros(length(MIS),1);
for i=1:length(MIS);
xfirst1=MIS(i,1);
xfirst2=MIS(i,2);
for j=1:79
Zhat(i)=Theta(1,j)+BFMS(j)*Theta(1,j+1);
end
end
end
I can’t run your code, so I can’t test this, but you might be able to get rid of the ‘j’ loop entirely and vectorise this:
j = 1:79;
Zhat(i)=Theta(1,j)+BFMS(j).*Theta(1,j+1);
That should be considerably more efficient than the explicit for loop. If it works (I believe it will, and have tested a prototype), you code then is:
j = 1:79;
Zhat=zeros(length(MIS),1);
for i=1:length(MIS);
xfirst1=MIS(i,1);
xfirst2=MIS(i,2);
Zhat(i)=Theta(1,j)+BFMS(j).*Theta(1,j+1);
end
This should be much easier to work with.
Dorothy
2017년 2월 5일
Walter Roberson
2017년 2월 5일
You should never use eval() on a symbolic expression. Symbolic expressions are written in a language that is not quite MATLAB.
You can subs() a symbolic expression to bring in the current numeric value of any symbolic variables it uses, and you can double() that result to convert it to numeric.
However, if you have a symbolic formula that you are planning to do that with, you are typically better off using matlabFunction() on the formula to convert it into a numeric anonymous function that you can then call in your loop.
Dorothy
2017년 2월 5일
카테고리
도움말 센터 및 File Exchange에서 Common Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!