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!