How to get column values using indexes
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello everyone, I hope you are doing well.
I have the following data, i want to seperate the four column based on the fifth column
for example if the fifth column has value 1 it the first four column values store in other matrix.
Similarly for fifth column value = 2 the first four column related to values=2 store in other matrix.
0 Commenti
Risposte (2)
KSSV
il 13 Mag 2022
Modificato: KSSV
il 13 Mag 2022
LEt A be your matrix.
C5 = A(:,5); % 5th column
M1 = A(C5==1,1:4) ;
M2 = A(C5==2,1:4) ;
4 Commenti
Stephen23
il 13 Mag 2022
" i want it to be automatic"
Numbered variable names are a sign that you are doing something wrong.
A container array (e.g. a cell array) would probably be the best approach.
Voss
il 13 Mag 2022
S = load('matlab.mat');
A = S.Dataset23;
result = splitapply(@(x){x(:,1:4)},A,findgroups(A(:,5)))
2 Commenti
Voss
il 13 Mag 2022
Modificato: Voss
il 13 Mag 2022
The cell array contains the matrices you need. There is no reason to store them as separate matrix variables. Just access them from the cell array as needed.
S = load('matlab.mat');
A = S.Dataset23;
result = splitapply(@(x){x(:,1:4)},A,findgroups(A(:,5)))
result{1} % get the 1st matrix
result{5} % get the 5th matrix
3*result{5} % get the 5th matrix and multiply it by 3 (or do whatever you need to do with it)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!