Non-singleton dimensions of the two input arrays must match each other using bsxfun

9 views (last 30 days)
Hi,
I have 4D data with dimensions mydata=[ 156 , 192 , 26 , 41]. I have also an array of values which I import in Matlab as .txt file. The array has dimensions array=[1, 41].
I am using the bsxfun like this:
mydata = bsxfun(@rdivide, mydata, mydata(:,:,:,find(array==0)));
I am keep getting the error but I don't understand why. Can someone help with that?
Thanks a lot in advanced.

Accepted Answer

Stephen23
Stephen23 on 14 Dec 2018
Edited: Stephen23 on 14 Dec 2018
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):
size(mydata)
ans = r c p 41
But apparently you have more than one (or zero) matches for your logical comparison:
find(array==0) % btw, FIND is not required here.
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:
  1. both are the same size on that dimension,
  2. 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.:
[1,2,3] + [9,8,7,6,5,4]
If they were both the same length or one of them were scalar it would work, otherwise MATLAB will throw an error.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!