mHelpMe
mHelpMe

Reputation: 6668

MATLAB syntax length

I'm reading some MATLAB trying to pick it up. The line below is probably rather simple but I do not understand it.

I understand length will give me the length of a vector, in this case a vector which is part of a struct, index_struct.data_incl.

The actual value of index_stuct.data_incl at run time is simply 1. What is confusing me is what is inside the brackets i.e. (index_struct.data_incl == 1)? I can't work out what this line is trying to do as simple as it may be!

int_var                 = length(index_struct.data_incl(index_struct.data_incl == 1));

Upvotes: 0

Views: 46

Answers (2)

Dan
Dan

Reputation: 45741

try this (but think of x as your index_struct.data_incl:):

x = [1 4 5 13 1 1]
length(x(x==1))

ans =

     3

It's just counting the number of elements of your x vector that are equal to 1

because x==1 evaluates to [1 0 0 0 1 1] and then using logical indexing x(x==1) evaluates to [1 1 1] whose length is 3;

It could have been written more simply as sum(index_struct.data_incl == 1)

Upvotes: 1

sebas
sebas

Reputation: 879

If I dont see the code I can only guess..., but I guess that index_struc.data_incl should be a vector, with length n meaning that you have the option to read until n files, and all the values of the array should be 0 at the begining, and when you read a file you change the corresponding position in the vector index_struc.data_incl from 0 to 1. After some time you can see how many of these files you have read using

int_var = length(index_struct.data_incl(index_struct.data_incl == 1));

because it will give to you the number of 1 in the vector index_struct.data_incl.

Upvotes: 0

Related Questions