MATLAB Programming Techniques, Extracting Portions of a table
2 ビュー (過去 30 日間)
古いコメントを表示
Hello,
in the course "Programming Techniques" theses lines show how you can extract portions of a table:
t2 = t1(6:15,[1 5 end-1:end])
or
t2 = t1(6:15,["A" "E" "N" "O"])
Why do I need
end-1:end
in the first version?
Lisa
0 件のコメント
採用された回答
Piyush Kumar
2024 年 6 月 18 日
The end keyword in MATLAB is a reference to the last element or position in an array, matrix, or table. You can learn more about the end keyword from this documentation link.
In the second version, "N" and "O" must be the last 2 columns which can be referenced by "end-1" and "end" too. These are just 2 ways to perform same task.
0 件のコメント
その他の回答 (1 件)
John D'Errico
2024 年 6 月 18 日
編集済み: John D'Errico
2024 年 6 月 18 日
Why do you need it? Well, I don't see the question in the course, and I am not enrolled in the course, so...
But what does that construct do? When you are indexing, the end keyword indicates the last element of a vector or array, in whatever dimension you are looking at. I'll try it out in an example.
V = primes(20)
What does V(end) do?
V(end)
Ah, do you see? It returns the last element. How about this one?
V(end-1:end)
end-1 is the next to last element, so that returns the final two elements. And this one?
V([1, 3, end-1:end])
So the frst element, the third one, and the final two elements.
When it is an array you are working with, TRY IT!
A = magic(7)
A([2 4],[1 end-1:end])
It extracted from rows 2 and 4. And in terms of the columns, it took columns 1 6 and 7. So the 1st column, and the last two columns, since A was a 7x7 array.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!