Selecting all rows if range of column values is greater than 10
1 view (last 30 days)
Show older comments
I am trying to select all rows if a column vaues is greater than 10:
Its like in SQL statemnt Select * From TableA WHERE col1 & col2 & col3.... > 10
Can matlab do the same or do i need a loop if so how do i go by?
Results = TableA(TableA(:,1), (TableA(:, 2:end) > 10))
0 Comments
Answers (1)
Arjun
on 24 Mar 2025
I understand that your goal is to select rows from a table where the values in columns 1, 2, and 3 are all greater than 10.
In MATLAB, you can use logical indexing to focus on columns 1, 2, and 3 for the condition check. By applying the "all" function along the second dimension, you ensure that only rows where all values in these columns are greater than 10 are selected.
You can do so by using the code specified below:
selectedRows = TableA(all(TableA{:, 1:3} > 10, 2), :);
% TableA(:, 1:3) > 10 creates a logical matrix where each element is true if the condition is met.
% all(..., 2) checks if all conditions are true across the specified columns (1 to 3) for each row.
% TableA(..., :) selects all columns of rows where the condition is true.
Kindly refer to the documentation of "all" for better understanding: https://www.mathworks.com/help/releases/R2021a/matlab/ref/all.html
I hope this helps!
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!