Indexed Assignment on the Right Side
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sinan Islam
am 17 Nov. 2020
Kommentiert: Sinan Islam
am 18 Nov. 2020
I am calling function f(x) that returns a matrix of 100 columns.
All what I need is just the 10th column. So I need one vector from the whole returned matrix.
I need to get rid of 99 columns and retain only the 10th column.
In Julia, this can be done by:
columnTen = f(x)[:,10]
I am dying to do the same thing in MATLAB.
Not sure why this simple operation seems impossible.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 17 Nov. 2020
columnTen = struct('fx', rand(7,20)).fx(:,10) %needs R2019b or later IIRC
columnTen = subsref(rand(7,20), substruct('()', {':', 10}))
But if you are permitted an initialization beforehand:
Col = @(M,n) M(:,n); %once
ColumnTen = Col(rand(7,20), 10)
Weitere Antworten (1)
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!