Search code examples
arraysmatlabvectorcell-array

How can I combine two arrays of vector components into an array of vectors in matlab


I have two arrays of the same size, ux and uy, which I want to combine into a cell array of vectors such that U(1,1) contains a vector composed of uy(1,1),ux(1,1) and `numel(U)=numel(ux)=numel(uy)'

The components ux and uy represent the unit vector of the image gradient. The component arrays are created by elementwise multiplication:

ux = I1x./I1mag;
uy = I1y./I1mag;

I need to be able to access each vector multiple times, and call them as arguments of dot and cross and making an array of vectors would be faster and more convenient than creating an ad hoc vector for each one on every iteration where it is called.

Thanks

Edit for further clarity:

suppose I have an array

uy = (1,2,3;4,5,6);

and another array of the same size

ux = (9,8,7;6,5,4);

I need the yx vectors, so for our example that's

([1,9], [2,8], [3,7]; [4,6], [5,5], [6,4])

What's the most efficient way to do that, please? I'm going to get dot products of each pixel with its neighbours and vice versa, so each vector will be used 16 times and the full arrays contain on the order of 10^4 or 10^5 elements...

Thanks for your continued help.


Solution

  • You can create two layers. One layer contains ux and other layer contains uy.

    ux = [10 8 7;6 5 4];
    uy = [1 2 3;4 5 6];
    
    xy(:,:,1) = ux; // [10 8 7;6 5 4]
    xy(:,:,2) = uy; // [1 2 3;4 5 6]
    
    aaa=xy(1,1,:); // [10 1]
    bbb=xy(1,2,:); // [8 2]
    
    dot(aaa,bbb)
    

    Result will be:

    82