To find the position of all the 1s in a logical array in MATLAB, you can use indexing in combination with loops and conditional statements. The following code snippet shows how to accomplish this:
main.m473 chars22 lines
In this example, we created a logical array A
, which looks like this:
main.m18 chars4 lines
Then, we found the size of the array using the size
function, and initialized an empty array named ones_pos
to store the positions of the 1s.
Next, we used nested loops to iterate through each element of the array. For each element, we checked if its value was equal to 1 using an if
statement. If it was, we appended its position to the ones_pos
array using the end+1
shorthand notation, which is equivalent to size(ones_pos,1)+1
. Finally, we displayed the positions of the 1s using the disp
function.
gistlibby LogSnag