The abbreviation of function cos and sin?
Show older comments
I have a long matrix and i waana abbreviate my funcion cos and sin. For exmaple i wanna see cos and sin like c and s on the command window.haw can i do that.
15 Comments
Roger Stafford
on 29 Dec 2012
That's not a good idea. It's too easy to inadvertently use 'c' and 's' for variable names, and the combination would give you some very strange results.
Walter Roberson
on 29 Dec 2012
Is this for Symbolic Toolbox ?
Image Analyst
on 30 Dec 2012
I'm going along with Roger on this. Another reason not to do it is that it would be a maintainability nightmare. Anyone going over your code later would say "What the heck was this guy thinking?" I'd actually go the opposite way - use longer variable names and function names because they will be more descriptive and easier for others to follow your code. I can't stand it when I'm going over code people posted and it looks like a jumbled alphabet soup of cryptic 1 and 2 letter variable names, especially when there are no comments to explain them.
Walter Roberson
on 30 Dec 2012
If this is symbolic toolbox work and the key is displaying the information, then just before displaying the formula, you could subs() in the c and s in place of cos and sin.
I can imagine, that the source code for the creation of a rotation matrix e.g. based on Euler angles, is easier to maintain, when the rows match in a line width with less than 80 characters:
s = @sin; % Abbreviation for readability
c = @cos;
M = [ ...
c(y).*c(x), s(y), -c(y).*s(x); ...
-c(z).*s(y).*c(x)+s(z).*s(x), c(z).*c(y), c(z).*s(y).*s(x)+s(z).*c(x); ...
s(z).*s(y).*c(x)+c(z).*s(x), -s(z).*c(y), -s(z).*s(y).*s(x)+c(z).*c(x)];
clear('c', 's'); % Clean up to avoid mistakes!
Of course temporary variables like |cx = cos(x)| would be more efficient in the sense of computing time and text width, but this is thought as an example only.
But without doubt, I agree with Roger's and Image Analyst's general recommendation to avoid too compact function names. As any rule, exceptions can be found and must be documented clearly and exhaustively in the code.
Denizcan Alaca
on 30 Dec 2012
Azzi Abdelmalek
on 30 Dec 2012
Edited: Azzi Abdelmalek
on 30 Dec 2012
Can you explain what is your matrix? Are you using symbolic functions?
Denizcan Alaca
on 30 Dec 2012
Edited: Denizcan Alaca
on 30 Dec 2012
Azzi Abdelmalek
on 30 Dec 2012
Edited: Azzi Abdelmalek
on 30 Dec 2012
Try this
syms x y
f=[cos(x)+2 sin(y);cos(y) sin(x)*x]
out1=sym(regexprep(char(f),'cos','c'))
out=sym(regexprep(char(out1),'sin','s'))
Denizcan Alaca
on 30 Dec 2012
Azzi Abdelmalek
on 30 Dec 2012
Edited: Azzi Abdelmalek
on 30 Dec 2012
Pays attention to other variables that possibly contain 'cos' and 'sin', they will also be replaced by c and s.
Denizcan Alaca
on 30 Dec 2012
Edited: Denizcan Alaca
on 30 Dec 2012
Azzi Abdelmalek
on 30 Dec 2012
What 'end' is doing in your code?
T01=[c(t1) 0 s(t1) 0 ; s(t1) 0 -c(t1) 0 ; 0 1 0 0.26 ; 0 0 0 1];
out1=sym(regexprep(char(T01),'cos','c'))
out=sym(regexprep(char(out1),'sin','s'))
Denizcan Alaca
on 30 Dec 2012
Edited: Denizcan Alaca
on 30 Dec 2012
Azzi Abdelmalek
on 30 Dec 2012
After Edit, there is no 'end'. Ok did you try the above code? what is wrong?
Answers (2)
Azzi Abdelmalek
on 29 Dec 2012
Edited: Azzi Abdelmalek
on 29 Dec 2012
C=@(x) cos(x)
Jan
on 29 Dec 2012
c = @cos;
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!