Can I index a matrix on the same line it's created?

2 views (last 30 days)
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.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 30 Nov 2021
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 Comments
Alexander Ketzle
Alexander Ketzle on 30 Nov 2021
The first one you posted would be acceptable, I probably could've made my intent a little more clear lol. I didn't realize you could do multiple on the same line by separating with ; as I had done a test with
clc, clear all, disp("test");
though the *fun family may come in handy for me later.
Also for clear all, I had been using it just as I had been doing a few other things at the same time and sometimes I've found other scripts I'm working with with the same variable names being open can sometimes cause weird behavior that I could probably find other ways to fix if I just use clear [filename]
thanks for the help!
Benjamin Kraus
Benjamin Kraus on 1 Dec 2021
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.
You can see the specifics on the doc page for clear.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!