How to get column values using indexes

4 ビュー (過去 30 日間)
Stephen john
Stephen john 2022 年 5 月 13 日
編集済み: Voss 2022 年 5 月 13 日
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.

回答 (2 件)

KSSV
KSSV 2022 年 5 月 13 日
編集済み: KSSV 2022 年 5 月 13 日
LEt A be your matrix.
C5 = A(:,5); % 5th column
M1 = A(C5==1,1:4) ;
M2 = A(C5==2,1:4) ;
  4 件のコメント
Stephen john
Stephen john 2022 年 5 月 13 日
@KSSV Using a Single line you manully type
M1 = A(C5==1,1:4) ;
M2 = A(C5==2,1:4) ;
M3=A(C5==3,1:4) ;
Stephen23
Stephen23 2022 年 5 月 13 日
" 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
Voss 2022 年 5 月 13 日
S = load('matlab.mat');
A = S.Dataset23;
result = splitapply(@(x){x(:,1:4)},A,findgroups(A(:,5)))
result = 5×1 cell array
{ 96×4 double} {303×4 double} { 83×4 double} { 87×4 double} { 17×4 double}
  2 件のコメント
Stephen john
Stephen john 2022 年 5 月 13 日
@_ Now you created a cell thats great, Then how can i save this as a matrix?
Voss
Voss 2022 年 5 月 13 日
編集済み: Voss 2022 年 5 月 13 日
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 = 5×1 cell array
{ 96×4 double} {303×4 double} { 83×4 double} { 87×4 double} { 17×4 double}
result{1} % get the 1st matrix
ans = 96×4
1.0e+09 * 0.0000 2.6001 -0.0000 0.0000 0.0000 2.6001 -0.0000 0.0000 0.0000 2.6001 -0.0000 0.0000 0.0000 2.5997 -0.0000 0.0000 0.0000 2.5998 -0.0000 0.0000 0.0000 2.6002 -0.0000 0.0000 0.0000 2.6003 -0.0000 0.0000 0.0000 2.6004 -0.0000 0.0000 0.0000 2.6005 -0.0000 0.0000 0.0000 2.5998 -0.0000 0.0000
result{5} % get the 5th matrix
ans = 17×4
1.0e+09 * 0.0000 2.6037 -0.0000 0.0000 0.0000 2.6042 -0.0000 0.0000 0.0000 2.6044 -0.0000 0.0000 0.0000 2.6038 -0.0000 0.0000 0.0000 2.6040 -0.0000 0.0000 0.0000 2.6043 -0.0000 0.0000 0.0000 2.6039 -0.0000 0.0000 0.0000 2.6036 -0.0000 0.0000 0.0000 2.6038 -0.0000 0.0000 0.0000 2.6045 -0.0000 0.0000
3*result{5} % get the 5th matrix and multiply it by 3 (or do whatever you need to do with it)
ans = 17×4
1.0e+09 * 0.0000 7.8111 -0.0000 0.0000 0.0000 7.8126 -0.0000 0.0000 0.0000 7.8131 -0.0000 0.0000 0.0000 7.8114 -0.0000 0.0000 0.0000 7.8121 -0.0000 0.0000 0.0000 7.8129 -0.0000 0.0000 0.0000 7.8117 -0.0000 0.0000 0.0000 7.8109 -0.0000 0.0000 0.0000 7.8114 -0.0000 0.0000 0.0000 7.8134 -0.0000 0.0000

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by