Multiplying vectors of uneven length and summing the result - arrays

How can I for every element of a vector calculate several elements from another vector?
For example, x=[1,2] and y=[1,2,3,4] then I need to multiply and sum each element of x with all elements of y, like so;
x = [1,2]
y = [1,2,3,4]
z = [x1*y,x2*y] = [x1*y1+x1*y2+x1*y3+x1*y4,x2*y1+x2*y2+x2*y3+x2*y4]
The vectors can have unlimited elements.

x = randi(10,3,1);
y = randi(10,4,1);
tmp = bsxfun(#times,x.',y); % Pre-R2016b
% tmp = x.'*y; % Post R2016b method
out = sum(tmp(:));
One can use either bsxfun or implicit expansion to create a matrix of numel(x) * numel(y) size and then sum over the flattened array to get to a final result.

You can multiply x with sum of y
result = x * sum(y)

Related

How to assign x to all elements in a vector for loop

So there's a vector with multiple elements consisting of numbers from 1-10. What I'm trying to do is create a for loop that computes the calculation of x^2 - x for every element in that vector and then sum up all the results.
numbers <- c(9,4,6,4,7,3,6,10,6,4,1,9,1,1,9,8,8,10,3,7,3,7,5,2,3,6,6,2,3,8,7,7,6,8,1,8,5,5,7,3,6,6,7,6,2,1,10,8,10,7)
x <- assign(x, numbers)
for(i in nrow(numbers)){
x[i] = (x[i]^2)-x[i]
y <- sum(x[i])
return(y)
}

fsolve with two sets of variables

fsolve solves a problem specified by
F(x)=0
for x. In my case, x is a 2-dimensional array of size m x n. Now suppose
that in addition to x, there is a vector y that needs to be solved for
as well. What is the best way to expand the number of variables fsolve
solves for, given that fsolve only allows for a single argument?
One solution, one that I would like to avoid, is to rewrite the problem in
terms of a single vector with entries of x and y stacked as in
x(1) ... x(m x n), y(1) ... y(m-1)
I would like to avoid this solution because the equations for the first set
of variables can be nicely defined in 2 dimensions.
Would it be possible to create a 3-dimensional array of size m x n x 2 where the first page (the 3rd dimension in Matlab terminology) is the original x-matrix and the entries of y appear on the second page? But then, what is if y is only of size m-1 so that the second page is largely empty? Would this be possible somehow?
EDIT
Here is a simplied version of the model with m=n=2.
The equations in the first set are
x(1,1)*dot(k1,y)-y(1) = 0;
x(1,2)*dot(k2,y)-y(1) = 0;
x(2,1)*dot(k1,y)-y(2) = 0;
x(2,2)*dot(k2,y)-y(2) = 0;
where k1 and k2 are two vectors of parameters.
When m=n=2, the second set of equations consists of a single equation of the form
y(1)*dot(c1,x(1,:)) + y(2)*dot(c2,x(2,:)) = 0
Where c1 and c2 are two vectors of parameters. The last entry of y, here y(2), is always a parameter.
You can combine them into a single vector but you don't need to modify F. For example suppose you have a function F(x, y) where x and y are arbitrarily sized then something like the following should work
function [x, y] = fsolve2(F, x0, y0)
% flatten x0 and y0 into a single column vector
xy0 = [x0(:); y0(:)];
% utility functions for recovering x and y from combined vector
xstart = 1;
xend = xstart + numel(x0) - 1;
xsize = size(x0);
getx = #(xy) reshape(xy(xstart:xend), xsize);
ystart = xend + 1;
yend = ystart + numel(y0) - 1;
ysize = size(y0);
gety = #(xy) reshape(xy(ystart:yend), ysize);
% define G which takes xy and calls F
G = #(xy) F(getx(xy), gety(xy));
xy = fsolve(G, xy0);
% unwrap xy into x and y
x = getx(xy);
y = gety(xy);
end
which could be called with
x, y = fsolve2(F, x0, y0);

Equation relating the specific index of a value in an array and the size of the array MatLab

I'm trying to come up with an equation that relates the index of a value within a 3D array, to the index of the same array but reshaped into a column vector.
Consider the following.
A = randi([1,10],3,2,2);
A2 = reshape(A,3*2*2,1);
A and A2 have the same number of elements, but the arrangement of the elements is different for each array. If I lay out a possible example for A and A2 here it is clear geometrically how each index lines up.
A(:,:,1) = [9 10; 10 7; 2 1]
A(:,:,2) = [3 10; 6 2; 10 10]
A2 = [9; 10; 2; 10; 7; 1; 3; 6; 10; 10; 2; 10]
Let's say n=1:1:3*2*2, this is an array that is the same length as A2 and numbers each of the elements. The value of A(2,2,2)=2 and has indices [i,j,k]=[2,2,2]. I would like to have an equation relating i, j, k, and n.
I've looked into the built-in functions ind2sub and sub2ind but it seems that I inadvertently shaped my i, j, and k coordinates (which correspond with real x, y, and z points) differently than how MatLab does. This makes it difficult for me to change everything now, and is why I need an equation.
The conversion between 3D index and linear (1D) index is given by:
n=i+(j-1)*M + (k-1)*M*N
The reverse can be obtained recursively as:
k = floor((n-1)/(M*N)) +1
n = n - (k-1)*M*N
j = floor((n-1)/M) + 1
i = n - (j-1)*M
I haven't tested it, but I think it will give you what you are expeccting.

Calculate similarity score between multidimensional arrays (Tensor)

I have two tensors A and B of sizes 1500 x 1000 x 500. how can I calculate similarity index between them?
I have applied the following formula:
relerr = frob((A - B))./frob(A);
It depends on your definition for similarity score.
Simply you can use Euclidean distance as follow:
x = 20;
y = 10;
z = 12;
A = randi([1, 10], x, y, z);
B = randi([1, 10], x, y, z);
C = (A-B).^2;
similarity = sqrt(sum(C(:)));
or you can define other similarity measures like Correlation coefficeint.

Summing elements from a vector, bounded by certain indices

I have a row vector x in Matlab which contains 164372 components. I now want to group these elements in another vector y, which has to contain 52 components. The first element of the vector y must be the average of the first 164372 / 52 = 3161 elements of the vector x, the second element of y must be the average of the next 3161 elements of x, etc. This continues until I have taken all of the 52 averages of the elements in the vector x and placed them in y.
How can I implement this in Matlab? Is there some built-in function that lets me sum elements from a certain index to another index?
Thank you kindly for any help!
With reshape and mean:
x = rand(1,164372); % example data
N = 52; % block size. Assumed to divide numel(x)
result = mean(reshape(x, numel(x)/N, []), 1)
What this does is: reshape the vector into a 52-row matrix in the usual column-major order, and then compute the mean of each column.

Resources