Getting logical indexes - MATLAB Cody - MATLAB Central

Problem 640. Getting logical indexes

Difficulty:Rate

This is a basic MATLAB operation. It is for instructional purposes.

---

Logical indexing works like this.

thresh = 4;
vec    = [1 2 3 4 5 6 7 8];
vi     = (vec > thresh)
vi =
       0     0     0     0     1     1     1     1

Once you have this TRUE FALSE vector (I call it vi: Valid Indices)

It can be used to get the values out:

big = vec(vi)
big =
       5     6     7     8

Given a vector, vec, and a value, v, return a binary vector that represents the indices where vector, vec, is equal to scalar, v.

Note, this works just as well with scalars and matrices.

----

To get the indices where this comparison is true, see this Cody problem.

Solution Stats

51.0% Correct | 49.0% Incorrect
Last Solution submitted on Jun 12, 2025

Problem Comments

Solution Comments

Show comments
Why should you share code?
In a discussion on LInkedin about my recent blog post, Do these...
2
3

Problem Recent Solvers1133

Suggested Problems

More from this Author51

Community Treasure Hunt

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

Start Hunting!