I got a 5 * 5 MATRICE.
I want to find the minimum of (i,j) which corresponds to minimum distance from a fixed point of my grid.
I used:
MATRICE(find(MATRICE(1,:))== min(MATRICE(1,:)),:)
and:
[minVal, minInd] = min(MATRICE(:,1));
MATRICE(minInd,1);
[~,MATRICE_2] = min(MATRICE(:,:));
disp(MATRICE_2) gets me the correct row(i) of the MATRICE but column(j) isn't displayed at all.
I am not sure if I understand correctly, but if you want to find the (row,col) indices of the smallest entry in your array, you can use this
[i,j] = ind2sub(size(MATRICE), find(MATRICE==min(MATRICE(:))))
where A is your matrix. This works by changing your matrix to a vector of 25 elements, looking for the position of the smallest value, and converting that position to a (row,col) pair by using ind2sub.
What you wrote finds minimum entry in every column of your matrix. So, every i entry in MATRICE_2 shows the row index of minimum value in the column i. In other words, column and row indices are
cols = 1:numel(MATRICE_2);
rows = MATRICE_2;
OK i figure it out:
[MinValue, MinIndex] = min(MATRICE(:));
MinSub =ind2sub(size(MATRICE), MinIndex);
MinValue is the minimum value of the MATRICE and MinIndex the index of it plus is pointing the fixed point directly.
Related
The problem is as follow:
I have a k-nearest index matrix. The column of the matrix denotes the number of an image (1:n). The row of index matrix denotes the index of k-nearest pixels correspoinding to the query pixel.
So I am using for-loops to find out whether a-th index is in column b: for example, 3 is in the first column (1-th pixel), then find out whether 1 is in 3-th column, if no, assign value, if yes, assign another value.
The code is here:
for pix = 1:row*col
for i = 1:k
position = find(Index(:, Index(i,pix)) == pix);
if isempty(position) difference = - weight(k, Index(i,pix)) ;
else difference = weight(i,pix) - weight(position, Index(i,pix)) ; end
end
end
Since row*col is very large. I hope to reduce the code into one for-loop. Is there any other method to speed up? Thanks in advance!
I have a 3D matrix d and I want to find the signed minimum value along the third dimension. Currently, I use following code
tmp = abs(d);
[row, col]=ndgrid(1:size(d,1),1:size(d,2));
[v,ind] = min(tmp,[],3);
index = row + size(d,1)*size(d,2)*(ind-1)+ size(d,1)*(col-1); %turn the ind to index
dm = d(index); %get the signed minimum value
The above code does not so efficient. Does anyone know a better choice? Thank you!
Rather than creating your grid of row and col values to convert ind into an index which you can use to index back into d, you can just use the first output of min which contains those minimum values along the third dimension.
dm = min(abs(d), [], 3);
I have this matrix
v=[4,-2,1;-2,1,-1;-2,3,6]
How can i return the max value of specific col with its row index ? Knowing that i used this function :
[amax,rowIdx]=max(abs(v(k:n,k)),[],1)
but it doesn't work well
here is my code :
v=[4,-2,1;-2,1,-1;-2,3,6]
n=3;
for k=1:n-1
[amax,rowIdx]=max(abs(v(k:n,k)),[],1)
end
If I understand your question correctly, you want to get maximum of third column?
[max_val, max_idx] = max(v(:, 3));
you select the third column from the matrix -> that gives you a single vector. max then operates on this vector and returns the max value together with its position, which is the row index in the original matrix.
I have created a function that should be able to read in any mxn matrix and gives me the maximum value of the entire matrix (not just per column) and what its indices are.
function [ BIGGEST ] = singlemax( x )
[Largest_values row]=max(x)
[biggest_number column] = max(max(x))
end
This function gives me all the information I need, however it is not very clean as it gets messy the larger the matrix.
The real problem area is printing out the row in which the maxima is located.
Largest_values =
0.7750 0.9122 0.7672 0.9500 0.6871
row =
3 2 3 2 2
biggest_number =
0.9500
column =
4
This is my print out given a random matrix as an input.With the function I have created I cannot read the indices of my max value in any given array using a created function. If I could somehow relate the maximas from each column and there corresponding row (such as making the results a matrix with the column max on top and the row index on bottom, all within the same respective columns )I could display the row of the absolute maximum.
Here's one approach:
value = max(x(:));
[rowIndex,columnIndex] = ind2sub(size(x),find(x==value));
Read the ind2sub documentation for more details.
Edited to modify so that it finds indices of all occurrences of the maximum value.
Im new to matlab and i having trouble understanding this line of code
A((i-1)*nneg+1:i*nneg,:) =
ones(nneg,1)*temp(i,2:n+1)+
temp(npos+1:npos+nneg,2:n+1);
does this mean that ->
each element in A where x is being varied between :
(i-1)*nneg+1 and the upper bound i*nneg and for all y, will have assigned 1* .....
an element from temp or all elements in the range of the y (temp(i,2:n+1))?
and by the same reasoning one of the range of temp(npos+1:npos+nneg,2:n+1) or all added up?
The command updates some horizontal sub-matrix of A
A(a:b, :) = some range of rows, and ALL columns = some horizontal sub-matrix of A
A(:, c:d) = some range of columns, and ALL rows = = some vertical sub-matrix of A
UPDATE:
Without seeing more of your code, i cant be sure but the syntax suggests that temp(npos+1:npos+nneg,2:n+1) is a matrix, and ones(nneg,1)*temp(i,2:n+1) is offcourse also a matrix of the same size which contains only 1's.
(i-1)*nneg+1 and i*nneg will both be integers, where (i-1)*nneg+1 <= i*nneg. these two integers define a sub-matrix of A which will have their values updated.
ones(nneg,1) creates a vertical array of ones [1,1,1,1...] with length nneg. this is then multiplies with a horizontal array temp(i,2:n+1) which creates a matrix X. X is added to another matrix temp(npos+1:npos+nneg,2:n+1), and the sub-matrix of A (explained above) is updated with this result.