Transposing matrix / Trouble understanding how bsxfun works

对着背影说爱祢 提交于 2019-12-12 21:22:05

问题


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.


回答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)



回答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)



回答3:


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.



来源:https://stackoverflow.com/questions/29982866/transposing-matrix-trouble-understanding-how-bsxfun-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!