Using logic to select specific values in a matrix

34 ビュー (過去 30 日間)
Kristine
Kristine 2025 年 11 月 22 日 20:06
コメント済み: Stephen23 2025 年 11 月 23 日 5:54
Hi y'all,
I am trying to create a second table from my original table based on taking all the values in column B, that fulfill the requirement for column A.
Example data:
Edit: attached file
I want to create a second table that only includes row 2 as long as the first row = 1.
This is what I tried:
A = SmallerSample
idnt = A(:,1) == 1 ;
new = A(ind1, 2);
The error I get:
Error using ()
A table row subscript must be a numeric array containing real positive
integers, a logical array, a character vector, a string array, a cell array of
character vectors, or a pattern scalar.
Error in Graph_Bead_Distributions
new = A(ind1, 2);

採用された回答

Walter Roberson
Walter Roberson 2025 年 11 月 22 日 22:02
A = SmallerSample
assuming that A is a table
idnt = A(:,1) == 1 ;
() subscripting of a table yields a table.
== has, somewhat recently, become defined for tables -- and the result is a table.
So the idnt variable is going to be a table.
new = A(idnt, 2);
You are then trying to index A using a table as the first subscript.
You should have used
idnt = A.(1) == 1;
or
idnt = A{:,1} == 1;

その他の回答 (1 件)

John D'Errico
John D'Errico 2025 年 11 月 22 日 20:48
編集済み: John D'Errico 2025 年 11 月 22 日 20:50
Well, you are doomed to failure, as long as you don't use the right variable names as you created them!
A = [1, 0.1234
1, 0.1345
1, 0.1456
2, 0.1567
2, 0.1678
2, 0.1789];
idnt = A(:,1) == 1 ;
You defined the variable idnt. But then you tried to use idn1. Which is a DIFFERENT variable name. Apparently you also had the variable idn1 defined in your workspace, as otherwise, you would have gotten a different error.
new = A(idnt, 2)
new = 3×1
0.1234 0.1345 0.1456
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
But using the name you defined, it does indeed work. So you were not really doomed to fail. You just need to be more careful in your code. Look more carefully at the error message next time, as it suggested there was problem with the variable ind1, which is not in your workspace as a valid variable to use as an index.
  2 件のコメント
Kristine
Kristine 2025 年 11 月 22 日 21:36
編集済み: Kristine 2025 年 11 月 22 日 21:37
I did misswrite it, but in my workspace both variables were equivalent, so I still get an error once correcting the mistake:
>> Graph_Bead_Distributions
Error using () (line 132)
A table row subscript must be a numeric array containing real positive integers,
a logical array, a character vector, a string array, a cell array of character
vectors, or a pattern scalar.
Error in Graph_Bead_Distributions (line 26)
new = A(idnt, 2);
Kristine
Kristine 2025 年 11 月 22 日 21:42
I think the example table I provided is not as helpful. The actual table I am using is where the issue lies.

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by