s='hello ', t='there you all'
They are really vectors but not as you have used so far. Try, for example,
size(s)
and you will get
ans = 1 7 % note the two spaces after 'o'
just like a vector.
x='12753'
sets x equal to a string of digits which is now not a number.
x(1:3)
abs(x) % returns the ascii value of each entry of the string
Now try, for example,
1+x
and you will get a strange answer which is '1' plus the ascii code of each entry in the string.
sort(x)
and the characters will be sorted to give 12357 and
fliplr(x)
These can be very useful in programming.
u=[s,t,' come back and see us, hear']
would build the string u= Hello there you all come back and see us, hear
num=findstr('a',u) %K=findstr(S1,S2) finds the locations of % occurrences of string S1 in string S2 length(num) % This will give the number of occurrences of a in u
n=17
defines n to be the integer 17 and
ns=int2str(n)
converts this integer to the string 17.
x=17.45
defines x to be the floating point number 17.45 and
xs=num2str(x)
converts this number to the string 17.45 which, remember, is now a string of characters, six of them to be exact.
eval([ns,'+',xs])
or
str2num(ns)+str2num(xs)
would give the number 34.45 which is now a real floating point number.
n=input(' input a positive integer, n = ') for i=1:n eval(['x',int2str(i),' = ',int2str(i^2)]) end
a=3 b=2.5 c=1 x=0:.05:4; plot(x,a*sin(b*x+c),2*(pi-1)/5,0,'o') grid xlabel('x-axis') ylabel('y-axis') title(['a = ',int2str(a),' b = ',num2str(b),' c = ',int2str(c)]) text(2*(pi-1)/5+.1,.1,'first zero')
v=input(' input a vector , v = ')
When you execute this you might do the following:
input a vector , v = [1 2 3 4]
then you will get v=[1 2 3 4]
input(' some test ', 's')
Note the additional comma and 's' to indicate that the input is to be considered as a string of characters.
i=input('Do you want to continue, Y/N [Y]:','s') if isempty(i) % the isempty command returns 1 (true) if i is not empty % and 0 if it is i='Y'; end
count=0; s='335' while str2num(s) ~= 495 count=count+1; m=sort(s); M=fliplr(m); d=eval([M,'-',m]); ds=int2str(d); lds=length(ds); if lds < 3 ds =[ds,'0'*ones(1,3-lds)]; end s=ds count end
p=[1 -6 11 -6]; x=0:.25:4; rand('normal'); % a string is used to declare normal distribution y=polyval(p,x)+rand(size(x)); c=polyfit(x,y,3) % interpolates a cubic poly to the data x, y % returns the 4 coefficients of the poly. fit=polyval(c,x); plot(x,fit,x,y,'o'); % a string is used to put circles at the data points
a=poly(1:5) % the a Wilkinson polynomial of degree 5, i.e., has roots 1,2,3,4,5 r=roots(a)
Now write a for that builds Wilkinson polynomials p1 p2 p3 p4 p5 p6 p7 p8 p9. Then plot the polynomials (showing their zeros and then saves the polynomials in files named fn1 fn2 fn3, etc. (hint: Suppose that
j=4 eval(['h',int2str(j),'=figure']) eval(['p',int2str(j),' = poly(1:j)']) eval(['x=.95:.05:(',int2str(j),'+.05);']) eval(['plot(x,polyval(p',int2str(j),',x))']) title(['Wilkinson polynomial of degree ',int2str(j)]) grid eval(['save fn',int2str(j),' p',int2str(j),' -ascii'])
format short % displays in 5-digit fixed point notation. format short e % displays in 5-digit floating point notation. format long % displays in 15-digit fixed point notation. format long e % displays in 15-digit floating point notation. % hex displays in hexadecimal notation.
fprintf('%.0e\n',pi) fprintf('%.1e\n',pi) fprintf('%.3e\n',pi) fprintf('%.5e\n',pi) fprintf('%.10e\n',pi) fprintf('%.0f\n',pi) fprintf('%.1f\n',pi) fprintf('%.3f\n',pi) fprintf('%.5f\n',pi) fprintf('%.10f\n',pi) fprintf('%.0g\n',pi) fprintf('%.1g\n',pi) fprintf('%.3g\n',pi) fprintf('%.5g\n',pi) fprintf('%.10g\n',pi) fprintf('%8.0g\n',pi) fprintf('%8.1g\n',pi) fprintf('%8.3g\n',pi) fprintf('%8.5g\n',pi) fprintf('%8.10g\n',pi)
clc disp([' ']') disp(['n log(n)/n 10^n*exp(n)']) disp('------------------------------') disp([' ']' ) fprintf('%.4g %.5f %.8g\n',8,log(8)/8,10^8*exp(8))