Solving Boolean Vector: Position of "1" Values

In summary, this code checks if the value at the kth position of the vector is 1. If it is, then the value at the kth position of the resulting array is set to res[k], and if it is not 1, then the value at the kth position of the resulting array is set to [] (empty).
  • #1
phillyj
30
0
Hi, I needed to write a function that takes a boolean vector and returns the position of the "1" values only. The correct way it was done was like this :
Code:
function res = tmp(x)
res=[ ];
for k=1:length(x)
if x(k) 
    res = [res,k];
end
end

I don't understand why the "if" statement returns only the value "1"? I thought that I might have to make a boolean statement that checks if the value at "k" is equal to "1" like this "if x(k) ==1"

Please help, it's very confusing to me
 
Physics news on Phys.org
  • #2
the function you are trying to write is a MATLAB function already. Try
Code:
find(x)
or read the documentation from
Code:
doc find
 
  • #3
trambolin said:
the function you are trying to write is a MATLAB function already. Try
Code:
find(x)
or read the documentation from
Code:
doc find

You misunderstand me. I know this is a built-in fuction but this was an exercise in learning MATLAB conditionals. Can you explain my original question on why the output works that way? My professor isn't around this week otherwise I might ask him.

Thanks
 
  • #4
actually your code works fine
I did this
Code:
x = [true false true]
res = [];
for k=1:length(x)
if x(k)
res = [res,k];
end
end
and the result is res = [1,3]
 
  • #5
trambolin said:
actually your code works fine
I did this
Code:
x = [true false true]
res = [];
for k=1:length(x)
if x(k)
res = [res,k];
end
end
and the result is res = [1,3]

Yes, the code works. I want to know why the if statement returns only the true values. I'm still learning. When I read the code in my mind this is what I understand: From index 1 to the end of the vector, result is index. Isn't that what
Code:
if x(k)
means? This makes no sense to me. Conditionals confuse me so much. Why aren't the false elements outputted. How does MATLAB differentiate between the true and false in this statement?
 
  • #6
"if x(k)" evaluates to true when x(k) is 1 (and possibly any other nonzero value), and evaluates to false when x(k) is 0.

This code could be written more clearly as

Code:
if x(k) == 1
  res = [res, k];
end
 
  • #7
Ah, now I got it. It works because you should read the if clause as follows

Code:
if (value that is declared here is nonzero)
...
end

For example
Code:
if 5
...
end
always executes but

Code:
if 0
...
end
never executes. See "doc if" in Matlab for details.
 

Related to Solving Boolean Vector: Position of "1" Values

What is a Boolean vector?

A Boolean vector is a data structure that contains only true (1) or false (0) values. It is used to represent logical statements or conditions in computer science and mathematics.

What does it mean to solve a Boolean vector?

Solving a Boolean vector means determining the position of the true (1) values within the vector. This can be useful in various applications, such as data analysis, pattern recognition, and machine learning.

How do you solve a Boolean vector?

To solve a Boolean vector, you can use various algorithms and methods depending on the specific problem and context. Some common approaches include using logical operations, machine learning techniques, and programming languages such as Python.

What is the significance of solving a Boolean vector?

Solving a Boolean vector can provide valuable insights and information about the data it represents. It can help identify patterns, relationships, and trends within the data, which can be used to make informed decisions and predictions.

Can solving a Boolean vector be applied to real-life problems?

Yes, solving a Boolean vector has many real-life applications, including data analysis, image and speech recognition, and natural language processing. It is also used in various industries such as finance, healthcare, and marketing to gain insights and make data-driven decisions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
595
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
959
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top