Can I index a matrix on the same line it's created?
2 次查看(过去 30 天)
显示 更早的评论
Hi y'all, I'm just trying to see if I can compress a script even further than I already have in hopes of potentially making it one line for no reason other than it'd be funny. Since I can put disp on the same line as clc, clear all; I was wondering if it was possible to index a matrix on the same line it's created, from this code:
clc, clear all;
coordinates = [6,2;-4,8;-5,-1;3,-7];
disp(table(coordinates(:,1),coordinates(:,2),sqrt(coordinates(:,1).^2 + coordinates(:,2).^2),(acosd(coordinates(:,1)./sqrt(coordinates(:,1).^2 + coordinates(:,2).^2)) .* sign(coordinates(:,2))),'VariableNames',{'X coordinate','Y coordinate','Arm Length (in)','Arm Angle (degrees)'}));
while yes I could just input the points manually, I want to see if there's a way to do it and be able to feed in any matrix I want quickly.
Thanks.
0 个评论
采纳的回答
Benjamin Kraus
2021-11-30
When I first read your question, I assumed this was not an acceptable answer:
clc, clear all; coordinates = [6,2;-4,8;-5,-1;3,-7]; disp(table(coordinates(:,1),coordinates(:,2),sqrt(coordinates(:,1).^2 + coordinates(:,2).^2),(acosd(coordinates(:,1)./sqrt(coordinates(:,1).^2 + coordinates(:,2).^2)) .* sign(coordinates(:,2))),'VariableNames',{'X coordinate','Y coordinate','Arm Length (in)','Arm Angle (degrees)'}));
However, you said: "Since I can put disp on the same line as clc, clear all", which suggests using semicolons to separate expressons on a single line is acceptable.
Regardles, I think the following accomplishes what you are trying to do, and does it fairly generically:
clc, clear, cellfun(@(coordinates) disp(table(coordinates(:,1),coordinates(:,2),sqrt(coordinates(:,1).^2 + coordinates(:,2).^2),(acosd(coordinates(:,1)./sqrt(coordinates(:,1).^2 + coordinates(:,2).^2)) .* sign(coordinates(:,2))),'VariableNames',{'X coordinate','Y coordinate','Arm Length (in)','Arm Angle (degrees)'})), {[6,2;-4,8;-5,-1;3,-7]})
There may be other ways to leverage the *fun family of functions (arrayfun, rowfun, cellfun, varfun).
Note: you can also do all sorts of tricks using eval, but that seems like it is cheating.
On this topic, you may find this series of blog posts interesting: Introduction to Functional Programming with Anonymous Functions: Part 1, Part 2, Part 3.
As an aside: clear all is overkill for most situations, and it can significantly slow down most scripts. For most purposes, clear is more than enough.
2 个评论
Benjamin Kraus
2021-12-1
A quick note about clear: Just clear alone will clear all variables in the workspace. Adding clear all does a lot more, including clearing MATLAB's cached memory of local scripts and functions, persistent variables, global variables, etc. This will slow down future commands, because they've got to rebuild some of that cache. Unless you are regularly using global variables (which is generally considered bad practice) or really need to clear persistant variables (this is more common), clear should be enough.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!