MATLAB localmax() function returns indexes in 1D vector - matlab

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).

Related

creating matrix from data-set

I have a network with edges and points. The FID shows the ID number of each edge that is created by Start_Point and End_Point. For example edge 3 is between the two points of 2 and 3 in the network.
FID Start_Point End_Point
1 1 2
2 1 4
3 2 3
4 2 4
I want to create a 4-by-4 matrix of these points. if there is an edge between 2 points the value is 1 else is inf:
[inf, 1, inf, 1;
1, inf, 1, 1;
inf, 1, inf, inf;
1, 1, inf, inf]
How can I create such a matrix in MATLAB?
You can convert it to a sparse matrix and then use full command to obtain adjacency matrix.
edges= [1 2;
3 4;
3 1
2 3];
n=size(edges,1);
% create sparse matrix with given edges and their reverse direction
A = sparse([edges(:,1); edges(:,2)],[edges(:,2); edges(:,1)],[ones(n,1); ones(n,1)]);
% create adjacency matrix
B=full(A);
% set zeros to inf
B(B==0)=inf;
and this is the result :
A =
(2,1) 1
(3,1) 1
(1,2) 1
(3,2) 1
(1,3) 1
(2,3) 1
(4,3) 1
(3,4) 1
>> B
B =
Inf 1 1 Inf
1 Inf 1 Inf
1 1 Inf 1
Inf Inf 1 Inf
Edit :
the sparse command create a sparse matrix with addressing values of its elements. One prototype for this command is as follow :
A=sparse(rows,cols,values);
for example A=sparse([1;2],[1,3],[10,5]) is a matrix which A(1,1)=10 and A(2,3)=5 and other elements are zero:
A=sparse([1;2],[1,3],[10,5]);
>> full(A)
ans =
10 0 0
0 0 5
In your case you have to add two directions to sparse matrix (symmetric) and all values are one. So you need to construct sparse matrix as :
A = sparse([edges(:,1); edges(:,2)],[edges(:,2); edges(:,1)],[ones(n,1); ones(n,1)]);
full command convert a sparse matrix to a dense one.
So you basically want to create an adjacency matrix from an adjacency list of edges? The number of your edges (i.e. your FID column) is irrelevant so I'm assuming your input data is of the form
edges = [1 2
1 4
2 3
2 4]
Now the first column of edges is the rows of your adjacency matrix and the second is the columns (or vice versa, it doesn't matter since your matrix is symmetrical)
The simplest solution is to use linear index which you would get via the sub2ind function:
adj = inf(size(edges,2));
idx = sub2ind(size(adj),edges(:,1), edges(:,2))
adj(idx) = 1;
I suspect your edges matrix will already be symmetrical but if it's not then just use
edges_sym = [edges; fliplr(edges)]
instead of edges
You can use accumarray:
edges1 = accumarray([startpoint endpoint]),1);
edges2 = edges1.'; % transpose your matrix, to obtain both edges
edges = edges1+edges2;
edges(edges==0)=inf;
accumarray gathers all points with common indices, pastes the value 1 on those indices. edges1 is the transpose of edges2, thus transpose, then add the two together. Find all indices on which the matrix is 0, then fill those values with inf.
Alternative:
edges= [1 2;
3 4;
3 1
2 3];
matrix = accumarray([edges;fliplr(edges)],1,[],[],inf);
fliplr flips your matrix left to right, to get all the desired combinations of indices. Then use accumarray to set a 1 on all locations specified by edges and put inf at the other locations.
If you are sure your matrix is symmetric, don't use fliplr, if you sure your matrix is non-symmetric, use fliplr and if you are not sure use this:
matrix = accumarray([edges;fliplr(edges)],1,[],#mean,inf);
where the #mean makes sure to set double entries to 1 anyway. For weighted edges do the following, where weights is an Nx1 array containing the weights and N is the number of edges.
matrix = accumarray([edges;fliplr(edges)],weights,[],#mean,inf);

Matlab: Solve Exponential Equation with two unknown parameters

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

How to calculate the number of occurrences of each element in the 100000 vectors using Matlab? [duplicate]

This question already has answers here:
How can I count the number of elements of a given value in a matrix?
(7 answers)
Closed 8 years ago.
For 100000 vectors containing 40 different numbers between 1 and 100, how to calculate the number of occurrences of each element in the 100000 vectors.
example:
A = [2 5 6 9]; B = [3 6 9 1]
the result should be if the numbers are between 1 and 10: [1 2 3 4 5 6 7 8 9 10, 1 1 1 0 1 2 0 0 2 0]
It seems like you want to compute the histogram of all values.
Use hist command for that
n = hist( A(:), 1:100 );
Assuming that you have a variable A that stores all of these vectors (like in Shai's assumption), another alternative to hist is to use accumarray. This should automatically figure out the right amount of bins you have without specifying them like in hist. Try:
n = accumarray(A(:), 1);
You can also use the sparse function to do the counting:
% 100000x1 vector of integers in the range [1,100]
A = randi([1 100], [100000 1]);
% 100x1 array of counts
n = full(sparse(A, 1, 1, 100, 1));
As others have shown, this should give the same result as:
n = histc(A, 1:100);
or:
n = accumarray(A, 1, [100 1]);
(note that I explicitly specify the size in the sparse and accumarray calls. That's because if for a particular vector A values didn't go all the way up to 100, then the counts array n will be shorter than 100 in length).
All three methods are in fact mentioned in the tips section of the accumarray doc page, which is the most flexible of all three.
The behavior of accumarray is similar to that of the histc function.
Both functions group data into bins.
histc groups continuous values into a 1-D range using bin edges.
accumarray groups data using n-dimensional subscripts.
histc returns the bin counts using #sum.
accumarray can apply any function to the bins.
You can mimic the behavior of histc using accumarray with val = 1.
The sparse function also has accumulation behavior similar to that of accumarray.
sparse groups data into bins using 2-D subscripts, whereas accumarray groups data into bins using n-dimensional subscripts.
sparse adds elements that have identical subscripts into the output. accumarray adds elements that have identical subscripts into
the output by default, but can optionally apply any function to the bins.

Method to sum of the elements of different sized matrix in Matlab

Can anybody help me to find out the method to sum of the elements of different sized matrix in Matlab ?
Let say that I have 2 matrices with numbers.
Example:
A=[1 2 3;
4 5 6;
7 8 9]
B=[10 20 30;
40 50 60]
I wanna create matrix C filling with sum(absolute subtract of matrix A and B).
Example in MS Excel.
D10=ABS(D3-I3)+ABS(E3-J3)+ABS(F3-K3)
E10=ABS(D4-I3)+ABS(E4-J3)+ABS(F4-K3)
F10=ABS(D5-I3)+ABS(E5-J3)+ABS(F5-K3)
And then (Like above)
D11=ABS(D3-I4)+ABS(E3-J4)+ABS(F3-K4)
E11=ABS(D4-I4)+ABS(E4-J4)+ABS(F4-K4)
F11=ABS(D5-I4)+ABS(E5-J4)+ABS(F5-K4)
Actually A is a 30x8 matrix and B is a 10x8 matrix.
How can i write this in Matlab?
Code
%%// Spread out B to the third dimension so that the singleton
%%// second dimension thus created could be used with bsxfun for expansion in
%%// that dimension
t1 = permute(B,[3 2 1])
%%// Perform row-wise subtraction and then summing of their absolute values
%%// as needed
t2 = sum(abs(bsxfun(#minus,A,t1)),2)
%%// Since the expansion resulted in data in third dimension, we need to
%%// squeeze it back to a 2D data
out = squeeze(t2)'

MATLAB: Matrix containing values of another matrix at specific indices

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));