Effacer les filtres
Effacer les filtres

MATLAB Programming Techniques, Extracting Portions of a table

3 vues (au cours des 30 derniers jours)
Elisabeth
Elisabeth le 18 Juin 2024
Modifié(e) : John D'Errico le 18 Juin 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

Réponse acceptée

Piyush Kumar
Piyush Kumar le 18 Juin 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.

Plus de réponses (1)

John D'Errico
John D'Errico le 18 Juin 2024
Modifié(e) : John D'Errico le 18 Juin 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)
V = 1x8
2 3 5 7 11 13 17 19
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
What does V(end) do?
V(end)
ans = 19
Ah, do you see? It returns the last element. How about this one?
V(end-1:end)
ans = 1x2
17 19
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
end-1 is the next to last element, so that returns the final two elements. And this one?
V([1, 3, end-1:end])
ans = 1x4
2 5 17 19
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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 = 7x7
30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A([2 4],[1 end-1:end])
ans = 2x3
38 27 29 5 36 45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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.

Community Treasure Hunt

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

Start Hunting!

Translated by