MATLAB Answers

Eval, sum and two loops!

8 views (last 30 days)
Dorothy
Dorothy on 5 Feb 2017
Commented: Dorothy on 5 Feb 2017
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 Comment

Stephen Cobeldick
Stephen Cobeldick on 5 Feb 2017
"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.

Sign in to comment.

Answers (1)

Star Strider
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.

  5 Comments

Show 2 older comments
Dorothy
Dorothy on 5 Feb 2017
Thank you but it does not work.
Let me explain my other values; I had a training data and I make use of some algorithms and built a model. For the model I need Theta and BFMS. I found them. (That was a big problem)
MIS is a matrix which includes x=xfirst1 and y=xfirst2 coordinates. These coordinates are belong to missing part, and we try to predict them as a Zhat value.
  • MIS(1,1)=158 (xfirst1)
  • MIS(1,2)=40 (xfirst2)
  • MIS(2,1)=159(xfirst1)
  • MIS(2,2)=39(xfirst2)...
BFMS are some functions and we will use them to calculate for every xfirst1, xfirst2 coodinates. BFMS is a function like that:
  • BFMS1= max(0, xfirst1 - 67)
  • ...
  • BFMS40= max(0, xfirst1 - 97)*max(0, xfirst2 - 114)
Another is Theta. Theta is a matrix and for instance
  • Theta(1,1)=-2.6722,
  • Theta(1,2)= 0.0121 ...
For example;
  • xfirst1=158
  • BFMS1= max(0, xfirst1 - 67)So eval(BFMS1) is equal to 91.
Zhat(1) will be the prediction for (158,40) Zhat(1)= -2,6722+ 91*0,0121 + ... +eval(BFMS79)*Theta(1,80);
at the end of these loops we will predict all Zhat values.
How will we use both eval and loop? That's the problem. In my view, we need one loop, one summation with threshold and have to use eval. I couldn't find any example with eval in a loop.
Walter Roberson
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.
Dorothy
Dorothy on 5 Feb 2017
Thank you Walter. I have a symbolic function so I should use matlabFunction(). I didnt use it before but I will search

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!