Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Is there a way to take a table of values and return a new table with only the rows that have data of a specified value?

2 ビュー (過去 30 日間)
Tyler Johnson
Tyler Johnson 2016 年 10 月 1 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
For example, lets say I have a table that contains two rows:
1,2,3,4,5
5,6,7,8,9
Is there a way to, if given the number 4, return a new table with only the first row: 1,2,3,4,5 in it?

回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 10 月 2 日
If you mean "array" instead of table() objects, then
YourArray( any(YourArray == 4, 2), :)
  1 件のコメント
Tyler Johnson
Tyler Johnson 2016 年 10 月 2 日
I mean a table where a variable is assigned to each column and each row has equal length. Or is it only possible to extract columns at a time from this type of table?

Image Analyst
Image Analyst 2016 年 10 月 2 日
Here is one way:
% Create table variable
columnNames = {'Col1', 'Col2', 'Col3', 'Col4', 'Col5'}
t = table([1;5], [2;6], [3;7], [4;8], [5;9], 'VariableNames', columnNames)
% Scan rows keeping track of which rows have a 4 in them.
for row = 1 : size(t, 1)
rowsWith4(row) = any(t{row, :} == 4, 2);
end
% Extract only rows with 4 in them.
newTable = t(rowsWith4, :)
You'll see
t =
Col1 Col2 Col3 Col4 Col5
____ ____ ____ ____ ____
1 2 3 4 5
5 6 7 8 9
newTable =
Col1 Col2 Col3 Col4 Col5
____ ____ ____ ____ ____
1 2 3 4 5

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by