any returns 0 eventhough there is a non zero element in the row

1 view (last 30 days)
I have a cell that looks like this:
measureables =
'GS_L' 'OECF' 'DYNAMIC_RANGE' 'NOISE'
I do this:
OECFINDEX = strcmp(measureables(:,:),'OECF');
which gives:
ans =
0 1 0 0
now I do this:
OECFINDEX = OECFINDEX(:,any(OECFINDEX));
which should give (correct me if I'm wrong): 1
but the answer is 0.
It does work for larger cells that look like this:
measureables =
measureables =
'GS_L' 'OECF' 'DYNAMIC_RANGE' 'NOISE'
'GS_L' 'OECF' 'DYNAMIC_RANGE' 'NOISE'
'GS_L' 'OECF' 'DYNAMIC_RANGE' 'NOISE'
'GS_L' 'DYNAMIC_RANGE' 'NOISE'
here the program gives me:
1
1
1
0
why is not working for a single column?

Accepted Answer

Thorsten
Thorsten on 7 Apr 2015
Because it's just a vector (or 1 x N matrix),
any(OECFINDEX)
returns a single number, namely 1 in your example, and because OECFINDEX has just one row,
OECFINDEX(:,any(OECFINDEX))
is the same as
OECFINDEX(:,1);
or
OECFINDEX(1)
which is 0 in your case.

More Answers (1)

Star Strider
Star Strider on 7 Apr 2015
I believe you’re using the wrong syntax with any.
Consider:
OECFINDEX = [0 0 1 0];
OECFINDEX = any(OECFINDEX)
produces:
OECFINDEX =
1

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!