Transposing matrix / Trouble understanding how bsxfun works
Asked Answered
G

3

2

This could be a weird question because Many would be wondering why to use such a complicated function like bsxfun for transposing while you have the .' operator.

But, transposing isn't a problem for me. I frame my own questions and try to solve using specific functions so that i learn how the function actually works. I tried solving some examples using bsxfun and have succeeded in getting desired results. But my thought, that i have understood how this function works, changed when i tried this example.

The example image i've taken is a square 2D image, so that i'm not trying to access an index which is unavailable.

Here is my code:

im = imread('cameraman.tif');
imshow(im);
[rows,cols] = size(im);

imout = bsxfun(@(r,c) im(c,r),(1:rows).',1:cols);

Error i got:

Error using bsxfun
Invalid output dimensions.

Error in test (line 9)
imout = bsxfun(@(r,c) im(c,r),(1:rows).',1:cols);

PS: I tried interchanging r and c inside im( , ) (like this: bsxfun(@(r,c) im(r,c),(1:rows).',1:cols)) which didn't pose any error and i got the same exact image as the input.


I also tried this using loops and simple transpose using .' operator which works perfectly.

Here is my loopy code:

imout(size(im)) = 0;

for i = 1:rows
    for j = 1:cols
        imout(i,j) = im(j,i);
    end
end

Answer i'm expecting is, what is wrong with my code, what does the error signify and how could the code be modified to make it work.

Greasy answered 1/5, 2015 at 6:59 Comment(5)
Nice question. Specially for correctly mentioning .' and not ' :-)Simba
@LuisMendo Thanks. I also did some test and found out a solution. i expect you guys to verify that and correct if i am wrong :)Greasy
I'm not sure I understand what you wrote in your answer. bsxfun does singleton expansion: it repeats each singleton dimension according to the size of that dimension in the other array. That's all there is to it, I thinkSimba
@LuisMendo, yeah.. doc says it virtually replicates the array along that dimension to match the other array.. If it had actually replicated, the size would be different.Greasy
Yes, sorry. The replication is only from a functioning point of view. It's not actually replacited (that would be repmat)Simba
E
1

The problem here is that your function doesn't return an output the same shape as the input it is given. Although the requirement for bsxfun is that the function operates element-wise, it is not called with scalar elements. So, you need to do this:

x = randi(5, 4, 5)
[m, n] = size(x);
bsxfun(@(r, c) transpose(x(c, r)), (1:n)', 1:m)
Electrosurgery answered 1/5, 2015 at 10:0 Comment(1)
"Although the requirement for bsxfun is that the function operates element-wise, it is not called with scalar elements". That made a lot of sense to me. I did some test and found out the results were exactly same as yours :) +1Greasy
P
2

You can use the anonymous function with bsxfun like so -

%// Create the tranposed indices with BSXFUN
idx = bsxfun(@(r,c) (c-1)*size(im,1)+r,1:rows,(1:cols).') %//'

%// Index into input array with those indices for the final transposed output
imout = im(idx)
Prine answered 1/5, 2015 at 7:32 Comment(3)
hmm :) That would work, +1 but still would you mention why my code isn't working? Also what does the error signify? why isn't matlab giving me the same error when i interchange r and c? That would make me understand the function little bit more. will u pls?Greasy
@SanthanSalai I wish I had a good answer to the error thing, unfortunately I don't.Prine
No prob :) i did some test with it and i guess i found out the reason for that error. i have posted it as a solution and i want you guys to verify that and correct if i am wrong.Greasy
E
1

The problem here is that your function doesn't return an output the same shape as the input it is given. Although the requirement for bsxfun is that the function operates element-wise, it is not called with scalar elements. So, you need to do this:

x = randi(5, 4, 5)
[m, n] = size(x);
bsxfun(@(r, c) transpose(x(c, r)), (1:n)', 1:m)
Electrosurgery answered 1/5, 2015 at 10:0 Comment(1)
"Although the requirement for bsxfun is that the function operates element-wise, it is not called with scalar elements". That made a lot of sense to me. I did some test and found out the results were exactly same as yours :) +1Greasy
G
1

I wanted to know how bsxfun works so i created a function like this:

bsxfun test function:

function out = bsxfuntest(r,c)
    disp([size(r) , size(c)]);
    out = r + c;  // just normal addition so that it works fine.
end

My script:

im = magic(5);

[rows,cols] = size(im);

bsxfun(@bsxfuntest ,(1:rows).',1:cols);

Output: (not the value of output of the function. These are those which are printed within bsxfuntest.m function using disp)

 5     1     1     1

 5     1     1     1

 5     1     1     1

 5     1     1     1

 5     1     1     1

Conclusion:

bsxfun passes each column into the function instead of each element.

If either one of the input is a scalar, then the function is called only one time i.e the matrix whether it is 2D or 3D or nD, is passed in one go.

Try this:

bsxfun(@bsxfuntest , repmat(5,[5 5 5]) ,1);

Also if both the inputs are of same dimensions, then also the function is called only one time.

Try this:

bsxfun(@bsxfuntest , repmat(5,[5 5 2]) , repmat(2,[5 5 2]))  

If none of them is a scalar, and both the inputs are of different dimensions, then The inputs are passed as column vectors.

Try this:

bsxfun(@bsxfuntest , repmat(5,[5 5 1]) ,permute(1:3,[1 3 2]));

and this:

bsxfun(@bsxfuntest , repmat(5,[5 5 2]) ,permute(1:2,[1 3 2]));    

Coming to the problem

>> im

im =

17    24     1     8    15
23     5     7    14    16
 4     6    13    20    22
10    12    19    21     3
11    18    25     2     9

Taking the code in the question:

imout = bsxfun(@(r,c) im(c,r),(1:rows).',1:cols);

When i try im(c,r) i.e im(1,(1:5).')

>> im(1,(1:5).')

ans =

17    24     1     8    15   

Here, bsxfun expects a column vector while the output is a row vector. I guess that is the reason why MatLab produces an error stating

Invalid output dimensions.

This was also the reason why i didn't get any error when i replaced r and c in the above code like this bsxfun(@(r,c) im(r,c),(1:rows).',1:cols). Because here, the output was a column vector itself.

So i tried to transpose the results to obtain the column vector like this:

>> imout = bsxfun(@(r,c) (im(c,r)).',(1:rows).',1:cols)

imout =

17    23     4    10    11
24     5     6    12    18
 1     7    13    19    25
 8    14    20    21     2
15    16    22     3     9

The code is exactly the same as Edrics's solution and it gives the expected results.

Greasy answered 1/5, 2015 at 13:40 Comment(4)
Makes some sense now! But, still few things are not exactly clear to me, need to dig deeper I think, to me at least. Also, would be interesting to see how this goes onto more dimensions.Prine
@Divakar, have you done any further research on this? any strange results? If positive, would you like to share, or may be post as a new topic? could be helpful for many of us.. eagerly waiting :)Greasy
Ah I am sorry, no I haven't. Maybe sometime in the future I might, but no plans as of now, sorry really :)Prine
@Prine Thats okay.. no prob :)Greasy

© 2022 - 2024 — McMap. All rights reserved.