简体   繁体   中英

Generating a vector of symbolic variables in Matlab

I want to generate a symbolic vector p , with each element a symbolic variable:

p = [p1; p2; ...; pn];

I don't want to type syms p1 p2 ... because I have ~100 such variables. Is there a way to generate them automatically?

Yup. Use sym like so:

p = sym('p', [100 1]);

This syntax will create a vector of symbolic variables where p is the first character followed by an integer. We wish to create 100 of them, and this will give you a symbolic vector from p1 up to p100 , or however many you want. Simply change the 100 to whatever number you want.

This is what p looks like:

>> p

p =

   p1
   p2
   p3
   p4
   p5
   p6
   p7
   p8
   p9
  p10
  p11
  p12
  p13
  p14
  p15
  p16
  p17
  p18
  p19
  p20
  p21
  p22
  p23
  p24
  p25
  p26
  p27
  p28
  p29
  p30
  p31
  p32
  p33
  p34
  p35
  p36
  p37
  p38
  p39
  p40
  p41
  p42
  p43
  p44
  p45
  p46
  p47
  p48
  p49
  p50
  p51
  p52
  p53
  p54
  p55
  p56
  p57
  p58
  p59
  p60
  p61
  p62
  p63
  p64
  p65
  p66
  p67
  p68
  p69
  p70
  p71
  p72
  p73
  p74
  p75
  p76
  p77
  p78
  p79
  p80
  p81
  p82
  p83
  p84
  p85
  p86
  p87
  p88
  p89
  p90
  p91
  p92
  p93
  p94
  p95
  p96
  p97
  p98
  p99
 p100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM