Hello, I've a problem! I have to insert in my matlab program a " for " cycle in wich the name of variables is a function of cycle index so the name of variables is variable. For Example: for i=1:n ALFA i =...... end. I want to stamp: ALFA 1=... ALFA 2=... ..... ALFA n=... How can I do?????? Thank u!!!
In article <1179169081.488869.62950@u30g2000hsc.googlegroups.com>, Molokovellocet <molokovellocet@fastwebnet.it> wrote: >Hello, I've a problem! I have to insert in my matlab program a " for " >cycle in wich the name of variables is a function of cycle index so >the name of variables is variable. For Example: >for i=1:n > ALFA i =...... >end. >I want to stamp: >ALFA 1=... >ALFA 2=... >.... >ALFA n=... >How can I do?????? Thank u!!! As far as I know, you have to use either eval or dynamic field names. eval([ sprintf('ALFA%d',i), '= whatever']) or ALFAs.(sprintf('ALFA%d',i)) = whatever But from what you have described so far, it would seem most natural to use a simple matrix: ALFA(i) = whatever This would probably also optimize more easily, possibly eliminating the need for the for loop. -- "It is important to remember that when it comes to law, computers never make copies, only human beings make copies. Computers are given commands, not permission. Only people can be given permission." -- Brad Templeton
On 14 Mag, 21:08, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote: > In article <1179169081.488869.62...@u30g2000hsc.googlegroups.com>, > > Molokovellocet <molokovello...@fastwebnet.it> wrote: > >Hello, I've a problem! I have to insert in my matlab program a " for " > >cycle in wich the name of variables is a function of cycle index so > >the name of variables is variable. For Example: > >for i=1:n > > ALFA i =...... > >end. > >I want to stamp: > >ALFA 1=... > >ALFA 2=... > >.... > >ALFA n=... > >How can I do?????? Thank u!!! > > As far as I know, you have to use either eval or dynamic field names. > > eval([ sprintf('ALFA%d',i), '= whatever']) > > or > > ALFAs.(sprintf('ALFA%d',i)) = whatever > > But from what you have described so far, it would seem most natural > to use a simple matrix: > > ALFA(i) = whatever > > This would probably also optimize more easily, possibly eliminating > the need for the for loop. > -- > "It is important to remember that when it comes to law, computers > never make copies, only human beings make copies. Computers are given > commands, not permission. Only people can be given permission." > -- Brad Templeton ok!thank u, I use "eval" and it's ok! Now i have another problem. In the "for" loop I have to insert a new variable "B" that is function of alfa. Now I write the for loop i've inserted: for i=1:n m=cos(seq(i)); n=sin(seq(i)); eval([ sprintf('alfa%d',i), '= [m^2 n^2 -2*m*n;n^2 m^2 2*m*n;m*n - m*n m^2-n^2]']) eval([ sprintf('psi%d',i), '= [m^2 n^2 -m*n;n^2 m^2 m*n;2*m*n -2*m*n m^2-n^2]']); eval([ sprintf('B%d',i), '= ('alfa%d',i)*Q*('psi%d',i) ']); end I want that: B 1=alfa 1*Q*psi 1 B 2=alfa 2*Q*psi 2 ..... B n=alfa n*Q*psi n
Molokovellocet: <SNIP crime against humanity... this hurts so much, i don't know where to begin... one of the remedies: B=cell(n,1); for i=1:n m=cos(seq(i)); n=sin(seq(i)); alfa=[<YOUR EXPRESSION1>]; psi=[<YOUR EXPRESSION2>]; B{i,1}=alfa*Q*psi; end us
Molokovellocet wrote: > > > On 14 Mag, 21:08, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) > wrote: >> In article > <1179169081.488869.62...@u30g2000hsc.googlegroups.com>, >> >> Molokovellocet <molokovello...@fastwebnet.it> wrote: >> >Hello, I've a problem! I have to insert in my matlab program a " > for " >> >cycle in wich the name of variables is a function of cycle index > so >> >the name of variables is variable. For Example: >> >for i=1:n >> > ALFA i =...... >> >end. >> >I want to stamp: >> >ALFA 1=... >> >ALFA 2=... >> >.... >> >ALFA n=... >> >How can I do?????? Thank u!!! >> >> As far as I know, you have to use either eval or dynamic field > names. >> >> eval([ sprintf('ALFA%d',i), '= whatever']) >> >> or >> >> ALFAs.(sprintf('ALFA%d',i)) = whatever >> >> But from what you have described so far, it would seem most > natural >> to use a simple matrix: >> >> ALFA(i) = whatever >> >> This would probably also optimize more easily, possibly > eliminating >> the need for the for loop. >> -- >> "It is important to remember that when it comes to law, > computers >> never make copies, only human beings make copies. Computers > are given >> commands, not permission. Only people can be given permission." >> -- Brad Templeton > > ok!thank u, I use "eval" and it's ok! Now i have another problem. > > In the "for" loop I have to insert a new variable "B" that is > function > of alfa. Now I write the for loop i've inserted: > > for i=1:n > > m=cos(seq(i)); n=sin(seq(i)); > eval([ sprintf('alfa%d',i), '= [m^2 n^2 -2*m*n;n^2 m^2 > 2*m*n;m*n - > m*n m^2-n^2]']) > eval([ sprintf('psi%d',i), '= [m^2 n^2 -m*n;n^2 m^2 m*n;2*m*n > -2*m*n m^2-n^2]']); > eval([ sprintf('B%d',i), '= ('alfa%d',i)*Q*('psi%d',i) ']); > > end > > I want that: > > B 1=alfa 1*Q*psi 1 > B 2=alfa 2*Q*psi 2 > .... > B n=alfa n*Q*psi n > > > > are you trying to do a vector of B or variable names B1 B2 B3?? what you have should work to name the variables B1 B2 B3, etc... if you want a vector just change the print to sprintf('B(%d)',i)
"Molokovellocet" <molokovellocet@fastwebnet.it> wrote in message news:1179225329.682827.108980@q75g2000hsh.googlegroups.com... > On 14 Mag, 21:08, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote: >> In article <1179169081.488869.62...@u30g2000hsc.googlegroups.com>, *snip* > ok!thank u, I use "eval" and it's ok! Now i have another problem. > > In the "for" loop I have to insert a new variable "B" that is function > of alfa. Now I write the for loop i've inserted: > > for i=1:n > > m=cos(seq(i)); n=sin(seq(i)); > eval([ sprintf('alfa%d',i), '= [m^2 n^2 -2*m*n;n^2 m^2 2*m*n;m*n - > m*n m^2-n^2]']) > eval([ sprintf('psi%d',i), '= [m^2 n^2 -m*n;n^2 m^2 m*n;2*m*n > -2*m*n m^2-n^2]']); > eval([ sprintf('B%d',i), '= ('alfa%d',i)*Q*('psi%d',i) ']); > > end > > I want that: > > B 1=alfa 1*Q*psi 1 > B 2=alfa 2*Q*psi 2 > .... > B n=alfa n*Q*psi n You _can_ do this, but you _shouldn't_. In my opinion, a version of the code using cell arrays, like alfa{k} [alfa{1} and alfa{2} instead of alfa1 and alfa2] is much more readable, both right when you're writing the code and six months later when you have to try to figure out what you were thinking. I recommend the cell array approach; wherever you try to use alfa1, use alfa{1} instead, and similarly for psi and B. For more information about why creating variable names like this dynamically is discouraged, read question 3.6 in the newsgroup FAQ: http://www.mit.edu/~pwb/cssm/matlab-faq.txt -- Steve Lord slord@mathworks.com
On 15 Mag, 13:25, dave <d...@bigcompany.com> wrote: > Molokovellocet wrote: > > > On 14 Mag, 21:08, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) > > wrote: > >> In article > > <1179169081.488869.62...@u30g2000hsc.googlegroups.com>, > > >> Molokovellocet <molokovello...@fastwebnet.it> wrote: > >> >Hello, I've a problem! I have to insert in my matlab > program a " > > for " > >> >cycle in wich the name of variables is a function of cycle > index > > so > >> >the name of variables is variable. For Example: > >> >for i=1:n > >> > ALFA i =...... > >> >end. > >> >I want to stamp: > >> >ALFA 1=... > >> >ALFA 2=... > >> >.... > >> >ALFA n=... > >> >How can I do?????? Thank u!!! > > >> As far as I know, you have to use either eval or dynamic field > > names. > > >> eval([ sprintf('ALFA%d',i), '= whatever']) > > >> or > > >> ALFAs.(sprintf('ALFA%d',i)) = whatever > > >> But from what you have described so far, it would seem most > > natural > >> to use a simple matrix: > > >> ALFA(i) = whatever > > >> This would probably also optimize more easily, possibly > > eliminating > >> the need for the for loop. > >> -- > >> "It is important to remember that when it comes to law, > > computers > >> never make copies, only human beings make copies. Computers > > are given > >> commands, not permission. Only people can be given > permission." > >> -- Brad > Templeton > > > ok!thank u, I use "eval" and it's ok! Now i have another problem. > > > In the "for" loop I have to insert a new variable "B" that is > > function > > of alfa. Now I write the for loop i've inserted: > > > for i=1:n > > > m=cos(seq(i)); n=sin(seq(i)); > > eval([ sprintf('alfa%d',i), '= [m^2 n^2 -2*m*n;n^2 m^2 > > 2*m*n;m*n - > > m*n m^2-n^2]']) > > eval([ sprintf('psi%d',i), '= [m^2 n^2 -m*n;n^2 m^2 m*n;2*m*n > > -2*m*n m^2-n^2]']); > > eval([ sprintf('B%d',i), '= ('alfa%d',i)*Q*('psi%d',i) ']); > > > end > > > I want that: > > > B 1=alfa 1*Q*psi 1 > > B 2=alfa 2*Q*psi 2 > > .... > > B n=alfa n*Q*psi n > > are you trying to do a vector of B or variable names B1 B2 B3?? what > you have should work to name the variables B1 B2 B3, etc... if you > want a vector just change the print to sprintf('B(%d)',i) I'm tryng to do n matrix called B1,B2,B3...Bn and each of them is function of Alfa and psi and in particular: B1=alfa1 *Q* psi1 B2=alfa2 *Q* psi2 B3=alfa3 *Q* psi3 ..... Bn=alfa n *Q* psi n
Hi Molokovellocet, Can you explain us why you don't want to listen to all good advices that have been given to you which say that EVAL=EVIL. Why this code doesn't fit your need ? : for i=1:n m=cos(seq(i)); n=sin(seq(i)); alfa{i}= [m^2 n^2 -2*m*n;n^2 m^2 2*m*n;m*n -m*n m^2-n^2] psi{i}= [m^2 n^2 -m*n;n^2 m^2 m*n;2*m*n-2*m*n m^2-n^2]; B{i} = alfa{i}*Q*psi{i}; end I think that all of us will be interested in understanding why you absolutly want to use eval and make your code unreadable ??
On 15 Mag, 16:13, "French caro" <caro95...@nospam.free.fr> wrote: > Hi Molokovellocet, > Can you explain us why you don't want to listen to all good advices > that have been given to you which say that EVAL=EVIL. > Why this code doesn't fit your need ? : > > for i=1:n > > m=cos(seq(i)); n=sin(seq(i)); > alfa{i}= [m^2 n^2 -2*m*n;n^2 m^2 2*m*n;m*n -m*n m^2-n^2] > psi{i}= [m^2 n^2 -m*n;n^2 m^2 m*n;2*m*n-2*m*n m^2-n^2]; > B{i} = alfa{i}*Q*psi{i}; > > end > > I think that all of us will be interested in understanding why you > absolutly want to use eval and make your code unreadable ?? Because it don't work!!!
On 28 Mag, 10:02, Molokovellocet <molokovello...@fastwebnet.it> wrote: > On 15 Mag, 16:13, "French caro" <caro95...@nospam.free.fr> wrote: > > > > > Hi Molokovellocet, > > Can you explain us why you don't want to listen to all good advices > > that have been given to you which say that EVAL=EVIL. > > Why this code doesn't fit your need ? : > > > for i=1:n > > > m=cos(seq(i)); n=sin(seq(i)); > > alfa{i}= [m^2 n^2 -2*m*n;n^2 m^2 2*m*n;m*n -m*n m^2-n^2] > > psi{i}= [m^2 n^2 -m*n;n^2 m^2 m*n;2*m*n-2*m*n m^2-n^2]; > > B{i} = alfa{i}*Q*psi{i}; > > > end > > > I think that all of us will be interested in understanding why you > > absolutly want to use eval and make your code unreadable ?? > > Because it don't work!!! Ok. I 'll try to explain in the best way! I insert manually " numbers. In fuction of this variable I have to build "n" matrix called A,B,C.So I have the matrix (not element!!) A1, A2,...An; B2,B2...Bn; C1,C2...Cn ; m=cos(i) and n=sin(i).ok? Now every element of the matrix A1 is: A1= cos(1)^2 * sin(1)^2 -2*cos(1)*sin(1)...etc (see the expression up) A2= cos(2)^2 * sin(2)^2 -2*cos(2)*sin(2)...etc.. ...... An= cos(n)^2 * sin(n)^2 -2*cos(n)sin(n)..etc.. B1= cos(1)^2 * sin(1)^2 -*cos(1)*sin(1)..etc ...... Bn=cos(n)^2 * sin(n)^2 -2*cos(n)sin(n)... The problem born when I want to build the "n" matrix called C1 to Cn because every element of this matrix is defined as: C1= B1*Q*A1 C2= B2*Q*A2 ..... Cn= Bn*Q*An The element C11 of the MATRIX C1 is equal to B11*Q11*A11; the element C12 = B12*Q12*A12. If I use the espression eval([ sprintf('A%d',i), '= [m^2 n^2 -2*m*n;n^2 m^2 2*m*n;m*n - m*n m^2-n^2]']) It work to define the matrix's A and B but when I have to define the "n" matrix's C it dont' work! I hope it be more clear. THANK YOU!
In article <1180340962.845131.130940@p77g2000hsh.googlegroups.com>, Molokovellocet <molokovellocet@fastwebnet.it> wrote: >Ok. I 'll try to explain in the best way! >I insert manually " numbers. >In fuction of this variable I have to build "n" matrix called A,B,C.So >I have the matrix (not element!!) A1, A2,...An; B2,B2...Bn; >C1,C2...Cn ; m=cos(i) and n=sin(i).ok? Who imposed the requirement that the matrixes must be called A1, A2, ... An, B1, B2, ... Bn, C1, C2, ... Cn ? Why is it not allowed to use cell arrays such as A{1}, A{2}, ... A{n}, B{1}, B{2}, ... B{n}, C{1}, C{2}, ... C{n} ? >If I use the espression >eval([ sprintf('A%d',i), '= [m^2 n^2 -2*m*n;n^2 m^2 2*m*n;m*n - >m*n m^2-n^2]']) If you use cell arrays instead of eval(), the code becomes much easier. If someone (such as an instructor or employer) told you that you *must* use A1, A2, ... An and so on, could you use A{1}, A{2}, ... A{n} and so on for the purposes of your computation, and then copy over to A1, A2, and so on afterwards? -- I was very young in those days, but I was also rather dim. -- Christopher Priest