table2arra​yで条件に合う変数だ​けを呼び出す方法

16 Ansichten (letzte 30 Tage)
E E
E E am 29 Jun. 2021
Kommentiert: E E am 1 Jul. 2021
csvfile<-readtable('csvfile.csv')
for n=1:3 
VarX=table2array(csvfile((csvfile.name == name{n}), ★★));
end
添付した16行×3列のcsvファイルを読み込み(変数はname, id, profit)、各nameの値(5つの観測値)ごとにid==1(実際はidは1,2,3,4,5まである)を満たすprofitのみを取り出して別のtableにしたいのですが、↑の★★の部分にどのようなコードを書けばよいでしょうか。実際にやりたいものはn=25なのでループの基本形はこのまま維持したいです。

Akzeptierte Antwort

Atsushi Ueno
Atsushi Ueno am 29 Jun. 2021
ループを回さなくてもテーブルの必要部分だけを別のテーブルにする事が出来ます。
name = {'c', 'a', 'b'}; % 仮に用意したけど結局使わなかった...
csvfile = readtable('csvfile.csv')
csvfile = 15×3 table
name id profit _____ __ ______ {'a'} 1 88.931 {'a'} 2 54.751 {'a'} 3 36.136 {'a'} 4 0.9208 {'a'} 5 12.689 {'b'} 1 88.881 {'b'} 2 79.403 {'b'} 3 76.824 {'b'} 4 84.733 {'b'} 5 27.439 {'c'} 1 22.737 {'c'} 2 53.316 {'c'} 3 15.616 {'c'} 4 6.589 {'c'} 5 59.825
for n = 1:3
%VarX = table2array(csvfile((csvfile.name == name{n}), ★★));
%実際にやりたいものはn=25なのでループの基本形はこのまま維持したい
%★★は列の選択なので、コロン(全列の意味)が良いのではないかと思います
end
%各name毎にid==1を満たすprofitのみを取り出して別のtableにしたい
VarX = csvfile(csvfile.id == 1, :) % ループ無しで可能、tableにしたいのに何故table2array?
VarX = 3×3 table
name id profit _____ __ ______ {'a'} 1 88.931 {'b'} 1 88.881 {'c'} 1 22.737
VarX{:, 2:3} % table2array(VarX(:,2:3))と同じ意味。文字列があると配列に変換出来ない為nameを除く
ans = 3×2
1.0000 88.9311 1.0000 88.8808 1.0000 22.7372
  1 Kommentar
E E
E E am 1 Jul. 2021
無事にできました。色々な選択肢をわかりやすく示してくださり、ありがとうございました🙇♀

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu table finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!