How to search for the same value found in one matrix in another matrix
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have 2 matrixes of equal size. I am finding the smallest value and its coordinates in the first matrix using the function
function [row, col, min_val] = findlocationofmin(matrixx)
% Find the location of the minimum value in a given matrix
[row,col] = find(matrixx==min(matrixx(:)));
[min_val] = min(matrixx(:));
end
This gives the row, column and value of the minimum value of the matrix. I then want to search within a radius of that row and column for the same value in the second matrix.
I hope this makes sense.
Thank you
Edit:As an output I want the row and colum of the same value in the second matrix.
For example:
I would run the code on my matrix and get. row = 10, col = 12, min_val = 3 from the first matrix.
I then want to search the second matrix for the value 3 in a 5x5 radius around (10,12) and output the coordinates of the value 3
Train1.mat contains the two matrices F1 and F2.
9 comentarios
Guillaume
el 14 de Nov. de 2018
"This gives the row, column and value of the minimum value of the matrix"
Not quite. The accurate version of that statement would be This gives the rows, columns and value of the minimum values of the matrix. If your minimum is present several time throughout the matrix, you'll get as many rows and columns.
In the case that the minimum is present several times, should the search be around each minimum or just one?
What is your definition of a radius? When you say a 5x5 radius, do you actually mean a 5x5 square centered on the point?
Respuestas (2)
Nick
el 14 de Nov. de 2018
Oke if i understand it correctly you basically have 1 matrix, find the minimum and its location. In the second matrix you want to find the exact same number in a certain predefined range and its location (5x5 matrix etc). Building on the code you provided you could go with this. Put it in a script with some test values:
% generate testing data
matrix1 = ones(100,100);
matrix2 = ones(100,100);
matrix2(10,10) = 0.1;
matrix1(12,11) = 0.1;
% call the functions
[row1,col1,min_val] = findlocationofmin(matrix1);
[row2,col2] = findlocation_in_searchgrid(matrix2, min_val, row1, col1, 5)
% same function as provided by example
function [row, col, min_val] = findlocationofmin(matrix)
[row,col] = find(matrix==min(matrix(:)));
[min_val] = min(matrix(:));
end
% find indices of value in second matrix
function [row, col] = findlocation_in_searchgrid(matrix, value,row,column,gridsize)
% if grid size is even it will add 1 to the grid size as it have to be uneven or you cannot
% have a center value - 1x1, 3x3, 5x5 etc
range = repfloor(gridsize/2);
% determine the range
minRow = row-range;
minCol = column-range;
maxRow = row+range;
maxCol = column+range
% additional checks to make sure you dont get errors near the borders, this will change the grid
% size to be smaller near the edges of the matrix
if minRow<1
minRow = 1;
end
if minCol<1
minCol = 1;
end
if maxRow>size(matrix,1)
maxRow = size(matrix, 1);
end
if maxCol>size(matrix,2)
maxCol = size(matrix,2)
end
% find indices
[rowIdx, colIdx] = find(matrix(minRow:maxRow, minCol:maxCol)==value);
% add the original offset to the indices so the location in the original matrix is known
% assign nan if the value was not found
if isempty(rowIdx) || isempty(colIdx)
row = nan;
col = nan;
else
row = minRow + rowIdx -1;
col = minCol + colIdx -1;
end
end
If you want to find the closest value to the first minimum if none of values in the search grid are equal to it you can use the absolute value of the difference and get the min coordinate of that. But this should give you an idea how to appreach the problem
0 comentarios
Guillaume
el 14 de Nov. de 2018
is there a way i could change it so I only recieve the first value?
You can tell find to return the first value:
[row, col] = find(matrix == min(matrix(:)), 1);
Or you can use the 2nd return value of min which is always the index of the first location
[min_val, min_loc] = min(matrix(:));
[row, col] = ind2sub(size(matrix), min_loc);
That 2nd option will be a lot faster as it scans the matrix only once.
As for finding the 2nd minimum in a square of side n (not a radus which implies a circle):
n = 5;
halfside = ceil(n/2);
statrow = max(row-halfside, 1); %make sure we don't use invalid indices
startcol = max(col-halfside, 1); %make sure we don't use invalid indices
submatrix = matrix(startrow:min(row+halfside, end), startcol:min(col+halfside))
submatrix(row+halfside, col+halfside) = Inf; %set the global minimum to Inf so it's no longer a minium
[min_val2, min_loc2] = min(submatrix(:)); %find second minimum
[row2, col2] = ind2sub(size(submatrix), min_loc2);
row2 = row2 + startrow - 1;
col2 = col2 + startcol -1;
Would be one way to do it.
Another option would be to use sort which would probably be faster.
0 comentarios
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!