Solving Boolean Vector: Position of "1" Values

  • Thread starter Thread starter phillyj
  • Start date Start date
  • Tags Tags
    Confused Loop
Click For Summary

Discussion Overview

The discussion revolves around understanding a MATLAB function designed to identify the positions of "1" values in a boolean vector. Participants explore the behavior of conditional statements in MATLAB, particularly focusing on the interpretation of boolean values and the output of the function.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant presents a function that iterates through a boolean vector and collects the indices of "1" values, expressing confusion about the behavior of the "if" statement.
  • Another participant suggests using the built-in MATLAB function find(x) as an alternative solution, but acknowledges the original intent of learning conditionals.
  • A participant confirms that the original code works correctly, providing an example and the resulting output.
  • Further clarification is sought regarding why the "if" statement only returns true values, with a participant expressing confusion about the differentiation between true and false elements in the context of the code.
  • One participant explains that the condition if x(k) evaluates to true for nonzero values and false for zero, suggesting that this behavior is consistent with MATLAB's handling of boolean expressions.
  • A later reply summarizes the understanding that the "if" clause can be interpreted as checking for nonzero values, providing examples to illustrate this point.

Areas of Agreement / Disagreement

Participants generally agree on the functionality of the code, but there remains some confusion regarding the interpretation of conditional statements in MATLAB. The discussion reflects a mix of clarifications and ongoing questions without a definitive resolution on the conceptual understanding of conditionals.

Contextual Notes

Participants express varying levels of understanding regarding MATLAB's boolean evaluations and conditionals, indicating a need for further exploration of these concepts.

phillyj
Messages
30
Reaction score
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
the function you are trying to write is a MATLAB function already. Try
Code:
find(x)
or read the documentation from
Code:
doc find
 
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
 
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]
 
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?
 
"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
 
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K