I need help solving an indexing problem. The assigned problem states: Two matrices (x and y) give the coordinates to form matrix B from matrix A. Produce the matrix B which contains the values of A at the given coordinates in x and y.
For instance:
x = [1 1 1; 2 2 1]
y = [1 2 1; 3 2 4]
%This would read as (1,1),(1,2),(1,1),(2,3),(2,2),(1,4)
% Given matrix:
A = [6 7 8 9; 10 11 12 13];
%This would give us this answer for B (using the coordinate scheme above):
B=[6 7 6; 12 11 9];
I'm guessing I need to use the find function in conjunction with a sub2ind function, but I'm not 100% sure how to translate that into working code. The only thing I can think of would be to do something like this:
B=((x(1),(y(1)), (x(2),y(2)).......
But that would only work for the defined matrix above, not a randomly generated matrix. I tried looking for a similar problem on the site, but I couldn't find one. Your help would be really appreciated!
You can't do it for randomly generated matrices, because you have to ensure that matrix A has lines and columns as required from the values of x and y.
In this case, you can write:
for i=1:length(x(:))
B(i)=A(x(i),y(i));
end
B=reshape(B,size(x));
Related
I am trying to compute the variance of elements which are organised in matrices (in MATLAB). As an example, let's be A and B two matrices 2x2.
My goal is to find the matrix V (2x2 as well), being the variance of each element of A and each element of B, that is:
Can somebody help me on this?
This is a very simple use case of the var function:
A = [1 2;
3 4];
B = [5 6;
7 8];
V0 = var(cat(3,A,B),0,3);
V1 = var(cat(3,A,B),1,3);
This results in:
V0 =
8 8
8 8
V1 =
4 4
4 4
What happens is that you concatenate your matrices along some unused dimension and then compute the variance along that dimensions.
NOTE: The example of 2 matrices is not very meaningful, but I'm assuming your actual dataset is larger, in which case you could use this method.
I'm operating on very big, 2D, sparse matrices in Octave. I have hit the linear indexing limit of 2^31 and need to go bigger.
The problem is I have two same-sized vectors of X and Y coordinates and would like to modify respective points without a loop.
I have already tried looping over one dimension, and arrayfun - both work, but suffer from serious perfomance issues.
Is there any way to do this without recompiling Octave for 64bit linear indexing?
Example of what I would like to achive:
A = [1 2 3; 4 5 6; 7 8 9];
x = [1 3]; y = [3 2];
B = getxy/setxy(A, x ,y) % [7 6]
In order to calculate local maximums of 2D matrix Y, I use this
[~, indices]= localmax(Y);
But the indices is 1D. How to convert it back to 2D so to access its corresponding element in Y?
From the documentation for localmax:
Linear indices of the nonzero values of lmaxima. Use ind2sub to
convert the linear indices to matrix row and column indices.
For example:
inputmatrix = ...
[3 2 5 3
4 6 3 2
4 4 7 4
4 6 2 2];
[~,indices] = localmax(inputmatrix,4,false);
[I, J] = ind2sub(size(indices), indices);
Edit: I should have clarified as well. As #LuisMendo mentions in the comments above, you can access the elements of Y directly with these linear indices by using Y(indices).
I have a Matrix called A. For example the following:
A = [1 2 3; 3 4 1; 2 4 4]
Now I have the following equation:
A(x,y) = (j^x)*(i^y)
j and i are normal values (dimension 1x1), not indices of a matrix. ^
Lets make an example:
A(1,1) = 1 (First value of the Matrix)
1 = (j^1)*(i^1)
And a second one:
A(1,2) = 3
3 = (j^1)*(i^2)
Is there a possibility to receive one solution for the two parameters using Matlab?
Here is some code that can find the best solution to your problem, if there is one. In this case, there is no reasonable solution, but defining A by M([4 2]) (for example) does work reasonably well.
A = [1 2 3; 3 4 1; 2 4 4] %// the A matrix
[C,R]=meshgrid(1:3) %// create matrices of row/column indices
M=#(xy) xy(2).^C.*xy(1).^R %// calculates matrix of elements j^x*i^y
d=#(xy) A-M(xy) %// calculates difference between A and the calculated i^x*y^j matrix
r=fsolve(#(xy) norm(d(xy)),[1 1]) %// use fsolve to attempt to find a solution
d(r) %// show resulting difference between target matrix and solution matrix
norm(d(r)) %// norm of that matrix
M(r) %// show the solution matrix
I have an equation like this
y = a*x+b;
I have sets of y and x
y = [1 2 3 4 5]
x = [6 7 8 9 10]
I want to find a and b, but not one solution; all solutions. I guess, I have to use polyfit, but I don't know how to do this and I don't understand why I have to use polyfit? Can you explain this to me?
From the polyfit documentation:
p = polyfit(x,y,n) finds the coefficients of a polynomial p(x) of
degree n that fits the data, p(x(i)) to y(i), in a least squares
sense. The result p is a row vector of length n+1 containing the
polynomial coefficients in descending powers:
So, you have data y at x-coordinates x, and you want to fit a first-degree polynomial to it. So use
p=polyfit(x,y,1);
and then p(1)=a and p(2)=b, or y=p(1)*x+p(2).
There are other ways to do this, but polyfit is very simple.