How I can extract data from a vector based on index
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rita
am 3 Jun. 2016
Beantwortet: Stephen23
am 11 Apr. 2022
I have an index and a vector I would like to have data from the vector which is not in the index!For example
A=[5 3 7 8 89 6]
index=[2 3 4 5]
Now I want to have B as my answer B=[5 6] Thanks!
0 Kommentare
Akzeptierte Antwort
Roger Stafford
am 3 Jun. 2016
Bearbeitet: Roger Stafford
am 3 Jun. 2016
B = A(setdiff((1:length(A)).’,index(:)));
or if you're sure A and index are row vectors just
B = A(setdiff(1:length(A),index));
Weitere Antworten (2)
Stephen23
am 11 Apr. 2022
A simple and efficient approach:
A = [5,3,7,8,89,6];
index = [2,3,4,5];
B = A;
B(index) = []
0 Kommentare
Johanna
am 21 Feb. 2022
I looking for the opposite command of setdiff. That is, taking the example from above, I want the answer to be B=[3 7 8 89].
In particular I'm actually looking for the mean of the values mentioned in my index vector. I tried mean(A(index)), however, this does not work.
I guess it has to be something like mean(A(setequal(1:length(A),index))), however, setequal does not exist. Thanks!
2 Kommentare
Stephen23
am 21 Feb. 2022
" I want the answer to be B=[3 7 8 89]."
A = [5,3,7,8,89,6];
index = [2,3,4,5];
B = A(index)
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!