1
votes

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.

2
Do you want the cell array to be numel(uy) x numel(ux)?Suever
Assuming ux has one column, [ux, uy]. Otherwise, [ux', uy']. Or, [ux; uy]'Salman
Thanks for the suggestions. I'll edit the post to clarify - I want numel(U) = numel(ux) = numel (uy), but each so cell of U contains a vector [uy, ux].Olly
Why do you want a cell array? If all of the elements are the same size, a multidimensional array would be simpler to deal with.beaker
I need to be able to call the vector for dot and cross products... being able to get something like dot(U(x,y), U(a,b)) would be handy. Otherwise I could call it as U1 = [U(x,y,1), U(x,y,2)] and then dot(U1, U2), but won't this be slower?Olly

2 Answers

1
votes

If you really want it to be a cell vector, where each element is a [1 x 2] vector, use mat2cell:

ux = rand(15,1);
uy = rand(15,1);
U = [ux, uy];
K = mat2cell(U,ones(size(U,1),1),2);

But as others have pointed out, U = [ux, uy] is sufficient, as you can just call U(1,:) to get the exact same result without having to worry about cells.

If you are wanting to alculate the dot product on the two vectors, arrayfun(@dot,ux,uy) does that job element-wise.

1
votes

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