The problem lies in the last dimension (here I marked the first three dimensions with r,c,p because their actual numeric values are unimportant for this discussion):
ans = r c p 41
But apparently you have more than one (or zero) matches for your logical comparison:
Lets say, for example, that your logical comparison has four matches, then you will get four indices... and so the second array input to bsxfun will have size (r,c,p,4), where r,c,p are exactly the same as mydata. But remember that mydata, the first array input to bsxfun, has size (r,c,p,41).
bsxfun allows two possible cases for any one dimension of the two input arrays:
- both are the same size on that dimension,
- one of them has size one (in which case it will be expanded to the size of the other array).
Do your arrays fit either of these two conditions? (hint: no).
Ignoring the other dimensions might make this clearer: it is as if you are trying to operate on two vectors with different lengths, e.g.:
If they were both the same length or one of them were scalar it would work, otherwise MATLAB will throw an error.