find max and its column and row in 2D matrix - matlab

Suppose I have matrix like this:
a = [ 2 5 4 7; 1 2 5 8; 2 3 4 5; 4 3 1 5]
what is the function to return the maximum and its column and row's index ?
For example, in my case that function should return maximum is 8, and column index is 4, and row index is 2

You can do it by using max twice:
[m irows]=max(a)
[mm icol]=max(m)
irow=irows(icol)
a(irow,icol)
Another solution is to unroll a to be a vector with a(:), use max, which will give you a single index that you then need to convert to row and column. you can easily figure out how it works by printing b(:) with b=[1,3;2,4] for instance.

Following on from #yoh.lej, you can use ind2sub to convert the output from find into co-ordinates:
m = magic(5);
[y, i] = max(m(:));
[r, c] = ind2sub(size(m), i)

Related

Extracting a vector from a matrix using vector of column indices

I have a matrix
A = [ 5 2
2 3
-4 7 ];
and a vector v = [1 2 1]. I want extract the vector B from A using the columns indexed by v. That is, B should look like
B = [ 5
3
-4 ];
I tried B = A(:,v) but that didn't work. Any simple way to do this?
You have the column subscripts. Generate the row subscripts and use sub2ind to get linear indices to retrieve desired elements of A.
B = A(sub2ind(size(A),1:numel(v),v))

How to code this matrix multiplication?

I have two matrices:
A = [1 2;
3 4;
5 6]
B = A'
The multiplication should take in the way as if row and column vector is extracted from both.
C = B(:,i) * A(i,:) such that for first instance (1st row and 1st column) the result would be:
[1 2;
2 4]
This will be summed up vertically to obtain [3 6]. This sum will give final answer 9. Likewise, 2nd row & 2nd column, 3rd row & 3rd column and so on if matrix size is higher.
This final scalar value will be used for comparing which row and its corresponding column has high yield.
Your required result is actually mathematically equivalent of:
sum(A,2).^2 %or sum(A,2) .* sum(A,2)
If A and B are not transpose of each other then:
sum(A,2).* sum(B,1).'
You can use sum:
result = sum(bsxfun(#times,sum(A,2), B.'),2);
Or in the recent version of MATLAB you can write:
result = sum(sum(A,2).*B.',2)
Previous answer:
You can use permute:
result = sum(reshape(permute(A,[2 3 1]) .* permute(A,[3 2 1]),[],size(A,1)));
Or in the case of A and B:
result = sum(reshape(permute(B,[1 3 2]) .* permute(A,[3 2 1]),[],size(A,1)));
result = [9 49 121]
Thanks to #TommasoBelluzzo and #SardarUsama .
If your Matrix is of Size Nx2, then one possible answer is
A.*A * [1;1] + 2*A(:,1).*A(:,2)

Octave: find the minimum value in a row, and also it's index

How would one find the minimum value in each row, and also the index of the minimum value?
octave:1> a = [1 2 3; 9 8 7; 5 4 6]
a =
1 2 3
9 8 7
5 4 6
This is hard to find in the documentation.
https://www.gnu.org/software/octave/doc/v4.0.3/Utility-Functions.html
octave:2> [minval, idx] = min(a, [], 2)
minval =
1
7
4
idx =
1
3
2
If A is your matrix, do:
[colMin, row] = min(A);
[rowMin, col] = min(A');
colMin will be the minimum values in each row, and col the column indexes.
rowMin will be the minimum values in each column, and row the row indexes.
To find the index of the smallest element:
[colMin, colIndex] = min(min(A));
[minValue, rowIndex] = min(A(:,colIndex))
Suppose X is a matrix
row, col = Row and Column index of minimum value
[min_value, column_index] = min(X(:))
[row, col] = ind2sub(size(X),column_index)
Given a matrix A of size m x n, you need to find the row number for the smallest value in the x column.
e.g A is 64x3 sized;
search_column = 3;
[val,row] = min(results(:,search_column),[],1);
#row has the row number for the smallest value in the 3rd column.
Get the values for the first and second columns for the smallest row value
column1_value = A(row,1);
column2_value = A(row,2);

Count rows of a matrix and give back an array

I would like to know how to count rows in an matrix in such a way that gives an output for each colum. for example:
X=[1 1 1;
5 5 5]
I would like to find a command that when I input the matrix X the answers is [2 2 2], so that it counts the number of rows per column.
I have already found nunel(X) but the answer is a scalar numel(X)=6, whereas I need per column.
size(X,1) will give you the number of rows in the matrix (a scalar). a matrix has only one number of rows, i.e. each column has the same number of rows.
however if you still want the number of rows per each column you can use:
X = [1 1 1;
5 5 5];
nrows = size(X,1);
ncols = size(X,2);
nrowsPerCol = repmat(nrows, [1 ncols]) % [2 2 2]
Each matrix object in MATLAB has height and width property.
In other words: each column has the same number of rows.
To get this value, use MATLAB's size function:
[numOfRows, numOfCols] = size(X);

MATLAB: Applying vectors of row and column indices without looping

I have a situation analogous to the following
z = magic(3) % Data matrix
y = [1 2 2]' % Column indices
So,
z =
8 1 6
3 5 7
4 9 2
y represents the column index I want for each row. It's saying I should take row 1 column 1, row 2 column 2, and row 3 column 2. The correct output is therefore 8 5 9.
I worked out I can get the correct output with the following
x = 1:3;
for i = 1:3
result(i) = z(x(i),y(i));
end
However, is it possible to do this without looping?
Two other possible ways I can suggest is to use sub2ind to find the linear indices that you can use to sample the matrix directly:
z = magic(3);
y = [1 2 2];
ind = sub2ind(size(z), 1:size(z,1), y);
result = z(ind);
We get:
>> result
result =
8 5 9
Another way is to use sparse to create a sparse matrix which you can turn into a logical matrix and then sample from the matrix with this logical matrix.
s = sparse(1:size(z,1), y, 1, size(z,1), size(z,2)) == 1; % Turn into logical
result = z(s);
We also get:
>> result
result =
8
5
9
Be advised that this only works provided that each row index linearly increases from 1 up to the end of the rows. This conveniently allows you to read the elements in the right order taking advantage of the column-major readout that MATLAB is based on. Also note that the output is also a column vector as opposed to a row vector.
The link posted by Adriaan is a great read for the next steps in accessing elements in a vectorized way: Linear indexing, logical indexing, and all that.
there are many ways to do this, one interesting way is to directly work out the indexes you want:
v = 0:size(y,2)-1; %generates a number from 0 to the size of your y vector -1
ind = y+v*size(z,2); %generates the indices you are looking for in each row
zinv = z';
zinv(ind)
>> ans =
8 5 9