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

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

Related

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

Delete a row and a column at the same time in a matrix

Is it possible to delete a row and a column of a matrix at the same time ?
Example:
M = magic(3)
M =
8 1 6
3 5 7
4 9 2
And with a one-liner remove the column 2 and the row 2 to obtain:
M =
8 6
4 2
You can supply an array of indices for the row and column indices and MATLAB will automatically return all permutations (usually an annoying feature, but a benefit here).
So you could do something like:
M([1 3], [1 3])
Or more flexible, you could use logical indexing
M([true false true], [true false true])
Or more generally (with potentially more rows and columns)
row = 2; % Could be an array of rows to exclude
column = 2; % Could be an array of columns to exclude
out = M(~ismember(1:size(M, 1), row), ~ismember(1:size(M, 2), column))

find max and its column and row in 2D matrix

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)

Finding maxima in 2D matrix along certain dimension with indices

I have a <206x193> matrix A. It contains the values of a parameter at 206 different locations at 193 time steps. I am interested in the maximum value at each location over all times as well as the corresponding indices. I have another matrix B with the same dimensions of A and I'm interested in values for each location at the time that A's value at that location was maximal.
I've tried [max_val pos] = max(A,[],2), which gives the right maximum values, but A(pos) does not equal max_val.
How exactly does this function work?
I tried a smaller example as well. Still I don't understand the meaning of the indices....
>> H
H(:,:,1) =
1 2
3 4
H(:,:,2) =
5 6
7 8
>> [val pos] = max(H,[],2)
val(:,:,1) =
2
4
val(:,:,2) =
6
8
pos(:,:,1) =
2
2
pos(:,:,2) =
2
2
The indices in idx represent the index of the max value in the corresponding row. You can use sub2ind to create a linear index if you want to test if A(pos)=max_val
A=rand(206, 193);
[max_val, idx]=max(A, [], 2);
A_max=A(sub2ind(size(A), (1:size(A,1))', idx));
Similarly, you can access the values of B with:
B_Amax=B(sub2ind(size(A), (1:size(A,1))', idx));
From your example:
H(:,:,2) =
5 6
7 8
[val pos] = max(H,[],2)
val(:,:,2) =
6
8
pos(:,:,2) =
2
2
The reason why pos(:,:,2) is [2; 2] is because the maximum is at position 2 for both rows.
max is a primarily intended for use with vectors. In normal mode, even the multi-dimensional arrays are treated as a series of vectors along which the max function is applied.
So, to get the values in B at each location at the time where A is maximum, you should
// find the maximum values and positions in A
[c,i] = max(A, [], 2);
// iterate along the first dimension, to retrieve the corresponding values in B
C = [];
for k=1:size(A,1)
C(k) = B(k,i(k));
end
You can refer to #Jigg's answer for a more concise way of creating matrix C

Matlab: Getting Random values from each column w/o zeros

I have a 2d matrix as follows:
possibleDirections =
1 1 1 1 0
0 0 2 2 0
3 3 0 0 0
0 4 0 4 4
5 5 5 5 5
I need from every column to get a random number from the values that are non-zero in to a vector. The value 5 will always exist so there won't be any columns with all zeros.
Any ideas how this can be achieved with the use of operations on the vectors (w/o treating each column separately)?
An example result would be [1 1 1 1 5]
Thanks
You can do this without looping directly or via arrayfun.
[rowCount,colCount] = size(possibleDirections);
nonZeroCount = sum(possibleDirections ~= 0);
index = round(rand(1,colCount) .* nonZeroCount +0.5);
[nonZeroIndices,~] = find(possibleDirections);
index(2:end) = index(2:end) + cumsum(nonZeroCount(1:end-1));
result = possibleDirections(nonZeroIndices(index)+(0:rowCount:(rowCount*colCount-1))');
Alternative solution:
[r,c] = size(possibleDirections);
[notUsed, idx] = max(rand(r, c).*(possibleDirections>0), [], 1);
val = possibleDirections(idx+(0:c-1)*r);
If the elements in the matrix possibleDirections are always either zero or equal to the respective row number like in the example given in the question, the last line is not necessary as the solution would already be idx.
And a (rather funny) one-liner:
result = imag(max(1e05+rand(size(possibleDirections)).*(possibleDirections>0) + 1i*possibleDirections, [], 1));
Note, however, that this one-liner only works if the values in possibleDirections are much smaller than 1e5.
Try this code with two arrayfun calls:
nc = size(possibleDirections,2); %# number of columns
idx = possibleDirections ~=0; %# non-zero values
%# indices of non-zero values for each column (cell array)
tmp = arrayfun(#(x)find(idx(:,x)),1:nc,'UniformOutput',0);
s = sum(idx); %# number of non-zeros in each column
%# for each column get random index and extract the value
result = arrayfun(#(x) tmp{x}(randi(s(x),1)), 1:nc);