How can I index through logicals?
1 回表示 (過去 30 日間)
古いコメントを表示
How can I index/loop through table(T) to get the rows with logical 1s in T.logical. Also, check if the logical is 0s, if so get the respective row with the string 'High' from T.Level?
T = table;
A = randi([0, 1], 5, 5);
c1 = A(:,1);
c2 = A(:,2);
c3 = A(:,3);
c4 = A(:,4);
c5 = A(:,5);
vert = vertcat(c1, c2, c3);
Types = {'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C'}';
Level = {'Low', 'Medium', 'High', 'Low', 'Medium', 'High', 'Low', 'Medium', 'High', 'Low', 'Medium', 'High', 'Low', 'Medium', 'High'}';
T.Types = Types;
T.logical = vert;
T.Level = Level;
TrueTable = table;
TrueTable.UniqueType = unique(T.Types);
numTypes = height(TrueTable);
TrueTable.Total = cell(numTypes, 1);
0 件のコメント
採用された回答
Voss
2023 年 7 月 10 日
編集済み: Voss
2023 年 7 月 10 日
Construct table T:
A = randi([0, 1], 5, 5);
vert = reshape(A(:,1:3),[],1);
Types = {'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C'}';
Level = {'Low', 'Medium', 'High', 'Low', 'Medium', 'High', 'Low', 'Medium', 'High', 'Low', 'Medium', 'High', 'Low', 'Medium', 'High'}';
T = table(Types,vert,Level);
T = renamevars(T,'vert','logical')
Get a new table (called rows) that contains the rows of T where logical is 1 or Level is 'High':
idx = T.logical | strcmp(T.Level,'High');
rows = T(idx,:)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!