Matrix Indexing from an Array of Numbers
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lauren Hopkins
am 26 Feb. 2016
Kommentiert: Lauren Hopkins
am 26 Feb. 2016
Hello:
I have a matrix Y = 55x236 of values and a separate matrix Z = [24 38 46 52 66] I would like to extract all of the rows of Y but ONLY the columns starting at the values in Z (plus the next 4 columns). So in other words I would like to extract from Y, columns 24-28, 38-42, 46-50, 52-56, and 66-70.
So far I have been doing this with a for loop but this seems like something that could be done much easier. I have tried something like A = Y(:, (Z:Z+4)) but this only gives me columns 24-28. If I have to index into Z to do this, wouldn't that still require a loop?
0 Kommentare
Akzeptierte Antwort
Roger Stafford
am 26 Feb. 2016
Bearbeitet: Roger Stafford
am 26 Feb. 2016
Assuming Z is a row vector
Y2 = Y(:,bsxfun(@plus,Z,(0:4).'));
Weitere Antworten (1)
dpb
am 26 Feb. 2016
One soluion; may be some "more better"...
idx=cell2mat(arrayfun(@colon,Z,Z+4,'uniform',0))); % column indices vector
A=Y(:,idx);
On can dispense with the temporary idx of course by folding into the indexing expression...
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!