Prev: GUI + embedded "keyboard"-mode box
Next: Dividing an image into 8X8 block and find histogram of each block
From: Dhruv on 28 May 2010 01:09 I need to perform symbolic differentiation but the number of variables keep changing Ideally I would like to declare say N vars x1, x2, x3 .... xN - Can someone help me with how to define these symbols automatically given a value of N? I tried using strings to declare the variables but was unsuccesful. Thanks, Dhruv
From: Sean on 28 May 2010 08:41 "Dhruv " <dhruv.singh(a)gmail.com> wrote in message <htnj5g$jo$1(a)fred.mathworks.com>... > I need to perform symbolic differentiation but the number of variables keep changing > > Ideally I would like to declare say N vars x1, x2, x3 .... xN - Can someone help me with how to define these symbols automatically given a value of N? > I tried using strings to declare the variables but was unsuccesful. > > Thanks, > Dhruv One way without loops: >>n = 10; >>nvec = num2str([1:n]'); >>xnvec = bsxfun(@strcat,'x',nvec); >>cxnvec = cellstr(xnvec); >>cxnvec = cellfun(@(x)x(~isspace(x)),cxnvec,'UniformOutput',false); >>syms(cxnvec{:}) -Sean
From: Alan B on 28 May 2010 10:28
"Sean " <sean.dewolski(a)nospamplease.umit.maine.edu> wrote in message <htodln$fuo$1(a)fred.mathworks.com>... > "Dhruv " <dhruv.singh(a)gmail.com> wrote in message <htnj5g$jo$1(a)fred.mathworks.com>... > > I need to perform symbolic differentiation but the number of variables keep changing > > > > Ideally I would like to declare say N vars x1, x2, x3 .... xN - Can someone help me with how to define these symbols automatically given a value of N? > > I tried using strings to declare the variables but was unsuccesful. > > > > Thanks, > > Dhruv > > One way without loops: > > >>n = 10; > >>nvec = num2str([1:n]'); > >>xnvec = bsxfun(@strcat,'x',nvec); > >>cxnvec = cellstr(xnvec); > >>cxnvec = cellfun(@(x)x(~isspace(x)),cxnvec,'UniformOutput',false); > >>syms(cxnvec{:}) > > -Sean Another way: n=10; names = regexp(sprintf('x%d/',1:n),'/','split'); syms(names{1:end-1}) |