howto find out if a vector contains only one 1 and other are 0? or how to check if every entry is the same?
e.g. i need to check if a vector contains zeros except only one 1 like:
(0 0 0 0 1 0 0 0 0) -> true
(0 0 0 0 0 0 0 0 1) -> true
(0 0 0 0 2 0 0 0 0) -> false
(0 0 1 0 1 0 0 0 0) -> false
You can use logical indexing, assuming your vector is
v:numel(v(v==1))returns the number of elements equal to 1 in your vector.In the same way, if you want to check if every value is the same you can use:
numel(unique(v))which returns the number of unique entries ofv.