MATLAB Programming Techniques, Extracting Portions of a table
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Elisabeth
am 18 Jun. 2024
Bearbeitet: John D'Errico
am 18 Jun. 2024
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 Kommentare
Akzeptierte Antwort
Piyush Kumar
am 18 Jun. 2024
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 Kommentare
Weitere Antworten (1)
John D'Errico
am 18 Jun. 2024
Bearbeitet: John D'Errico
am 18 Jun. 2024
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 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!