How can I index through logicals?

R
Rookie Programmer · Jul 12, 2023 · 2.3K views
Question
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);  
Expert Answer
Profile picture of Neeta Dsouza
Neeta Dsouza PhD Expert
Answered Aug 20, 2026

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')
T = 15×3 table
    Types    logical      Level   
    _____    _______    __________

    {'A'}       1       {'Low'   }
    {'B'}       0       {'Medium'}
    {'C'}       1       {'High'  }
    {'D'}       0       {'Low'   }
    {'A'}       1       {'Medium'}
    {'B'}       0       {'High'  }
    {'C'}       1       {'Low'   }
    {'D'}       1       {'Medium'}
    {'A'}       0       {'High'  }
    {'B'}       1       {'Low'   }
    {'C'}       1       {'Medium'}
    {'D'}       0       {'High'  }
    {'A'}       0       {'Low'   }
    {'B'}       1       {'Medium'}
    {'C'}       1       {'High'  }

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,:)
rows = 12×3 table
    Types    logical      Level   
    _____    _______    __________

    {'A'}       1       {'Low'   }
    {'C'}       1       {'High'  }
    {'A'}       1       {'Medium'}
    {'B'}       0       {'High'  }
    {'C'}       1       {'Low'   }
    {'D'}       1       {'Medium'}
    {'A'}       0       {'High'  }
    {'B'}       1       {'Low'   }
    {'C'}       1       {'Medium'}
    {'D'}       0       {'High'  }
    {'B'}       1       {'Medium'}
    {'C'}       1       {'High'  }

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!