Indexing directly on a matrix literal
2 views (last 30 days)
Show older comments
I'm assuming it's not possible in MATLAB to index directly on a matrix literal.
For example: Say I want to show the first 5 rows of all these vectors:
>> [t_sim, u_sim, y_sim, y_data](1:5,:)
[t_sim, u_sim, y_sim, y_data](1:5,:)
↑
Error: Invalid expression. When calling a function or indexing a variable, use
parentheses. Otherwise, check for mismatched delimiters.
This works of course but requires an extra line and creating a variable:
>> temp = [t_sim, u_sim, y_sim, y_data];
>> temp(1:5,:)
ans =
0 -1.0000 0 0.3565
1.0000 1.0000 -0.9614 1.4867
2.0000 -1.0000 -0.6378 -1.5364
3.0000 1.0000 -1.2231 0.0902
4.0000 1.0000 -0.5430 -0.4092
>> clear temp
Presumably the MATLAB language is not parsed in a way that indexing is applied to "whatever comes before". Does that mean you can only index on a variable as above?
Can someone please confirm?
0 Comments
Answers (1)
Divya Yerraguntla
on 6 Feb 2020
Hi Bill,
You could have a look at a similar question : https://www.mathworks.com/matlabcentral/answers/78242-indexing-of-fresh-array-in-one-line-without-intermediate-variable
Hope it helps!
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!