8 views (last 30 days)

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!

Star Strider
on 5 Feb 2017

Edited: Star Strider
on 5 Feb 2017

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.

Walter Roberson
on 5 Feb 2017

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.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/323457-eval-sum-and-two-loops#comment_426216

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/323457-eval-sum-and-two-loops#comment_426216

Sign in to comment.