finding max value in each column and row - matlab

Say you have a 2D matrix,Is there a particular algorithm or way to do this in excel or matlab that would find the max of each row and column, such that each column and each row has only one maximal number N, where summing all N would result in the largest possible sum,i.e. is a row or column has a max number that is repeated., as such with the simple example below
1 2 4
3 1 4
1 2 4
the out put would be
1 2 4
3 2 4
1 2 4

You are looking for the maximum bipartite matching in a (complete) graph, where your matrix represents the edge weights matrix. You can compute this value using the Hungarian algorithm (MATLAB implementation available for download from File Exchange). Since you want the maximum match, negate all the numbers in your matrix and feed it to this function. You will get back two outputs - one is the (negative of) the maximum sum and the other a binary matrix with ones where the maximal elements occur in each row and column and zeros everywhere else.

Related

How do I save the indices of the top 4 maximum numbers in a matrix in scilab

I need to save the indices of 4 maximum numbers
for example, I need to get the indices of rows 10,9,7,5
5.0259327
4.7127487
4.8435524
4.8538644
5.1048996
6.2441973
5.9413803
6.2912638
5.1117512
5.8309519
5.7419509
6.9663477
5.9958319
6.9519781
6.5802736
6.7327558
7.6765878
I have used
[mA,nA]=max(distA)
where mA is the row and nA is the column
in getting one maximum number but I cannot figure out how to choose another maximum number without duplication. I cannot sort because I need the indices.
You can use the gsort function:
[S,ind]=gsort(distA,"g","d");
The index of the 4 largest elements is the given by
ind(1:4)
You can use this little trick.
[output_val, output_index] = max(input_mat(input_mat < max(input_mat)))
This will give you the value and index of the second largest element. And then similarly, you can do it for 4 numbers.

Get numbers in a matrix in different set positions

I have a matrix 1x5000 with numbers. Now I am interested in getting values from the matrix in different positions, more precisely in six different places of the matrix. The places should be based on the length, these are the numbers I want to get out:
Number in 1/6 of the matrix length
Number in 2/6 of the matrix length
Number in 3/6 of the matrix length
Number in 4/6 of the matrix length
Number in 5/6 of the matrix length
Number in 6/6 of the matrix length
These values could be printed out in another matrix, so assume the matrix is 1x5000, 3/6 would give the number in the middle of the matrix. I am new in Matlab and therefore the help is much appreciated!
Since your question is unclear I can try to give you an example.
First of all you can use numel function to get matrix's size.
It's easy to get necessary element in Matlab: you can address directly to any element if you know its number (index). So:
x(100) returns 100th element.
Now you got size and know what to do. Last moment - what to do if numel(x) / 6 return non integer?
You can use rounding functions: ceil, floor or round.
index = ceil(numel(x)/6) %if you want NEXT element always
result = x(index)
Next step: there are a lot of ways to divide data. For example now you have just 6 numbers (1/6, 2/6 and so on) but what if there are 1000 of them? You can't do it manually. So you can use for loop, or you can use matrix of indexes or perfect comment Stewie Griffin.
My example:
divider = [6 5 4 3 2 1] % lets take 1/6 1/5 1/4 1/3 1/2 and 1/1
ind = ceil( numel(x)./divider)
res = x(ind)
The colon notation in MATLAB provides an easy way to extract a range of elements from v:
v(3:7) %Extract the third through the seventh elements
You could either manually input range or use a function to convert fractions into suitable ranges

Matlab - show corresponding values of 1st column of matrix, baring in mind the last 2 values of the 2nd column

I created a matrix with two columns, using two vectors, f and PS, that I already had:
M = [f PS]; %81x2 matrix
And then I sorted the matrix with respect to "PS" (second column), in order to have the maximum values of "PS" at the last positions of the vector:
M1=sortrows(M,2); %81x2 matrix
And I got something like this:
f...PS
5...83
10...136
3...357
1...985
Since now I assured the last two values of "PS" are indeed the maximum values, the information I want to give to the user is 1 and 3 (f values corresponding to the 2 maximum values of PS, which are at the bottom).
How can I do this?
You can use the end index: M1(end,1) and M1(end-1,1) should contain 1 and 3, respectively.
Best,

how to sum matrix entities as indexing by another vector values?

Suppose I have a vector B=[1 1 2 2] and A=[5 6 7 4] in the form of B says the numbers in the A that are need to be summed up. That is we need to sum 5 and 6 as the first entry of the result array and sum 7 and 4 as the second entry. If B is [1 2 1 2] then first element of the result is 5+7 and second element is 6+4.
How could I do it in Matlab in generic sense?
A fexible and general approach would be to use accumarray().
accumarray(B',A')
The function accumulates the values in A into the positions specified by B.
Since the documentation is not simple to understand I will summarize why it is flexible. You can:
choose your accumulating function (sum by default)
specify the positions as a set of coordinates for accumulation into ND arrays
preset the dimension of the accumulated array (by default it expands to max position)
pad with custom values the non accumulated positions (pads with 0 by default)
set the accumulated array to sparse, thus potential avoiding out of memory
[sum(A(1:2:end));sum(A(2:2:end))]

Size function in matlab

I am a new in MATLAB and I have a problem understanding the function size
in this statement: for i=1:size(scale,2) WHERE scale can be any integer number .e.g scale=5.
I found that in MATLAB help size(A,1) returns the number of rows of A , and
size(A,2) returns the number of columns of A.
Now I'm really confused as to what is the functionality of (size).
As you know, matlab deals mainly with matrices. So, the size function gives you the dimension of a matrix depending on how you use it. For example:
1. If you say size(A), it will give you a vector of size 2 of which the first entry is the number of rows in A and the second entry is the number of columns in A.
2. If you call size(A, 1), size will return a scalar equal to the number of rows in A.
3. If you call size(A, 2), size will return a scalar equal to the number of columns in A.
A scalar like scale in your example is considered as a vector of size 1 by 1. So, size(scale, 2) will return 1, I believe.
Hope this clarifies.
The Linear Algebra operations in Matlab/octave by default follow Row-Column order (ie they are row major by default); so if A is a matrix of size 3x2 (3 rows and 2 columns), we can use size to determine the order of matrix/vector
size(A) will return 3 2 (the first entry representing no.of rows & the second one is no.of columns). Similarly,
size(A,1) returns 3 (1 here represents the no. of rows and A has 3 rows)
size(A,2) returns 2 (2 here represents the no. of columns and A has 2 columns)