the problem I'm working on is to find the saddle points of a matrix and now I'm trying this ...
nested loop to check every element
check if the element is the smallest in its column and the biggest in its row
if it is,print it into the matrix'indices'
if there was none,print empty matrix....and the code [row,col]=size(matrix); for I=1:row for j=1:col if matrix (I,j)==max(matrix, I)&&matrix (I,j)==min(matrix, j) indices=[i j;]: else indices=[ ] end end endsome help with the syntax please,thanks!!
Here is a simple approach. Note I define a saddle point as one that is either the largest in its column and smallest in its row or the smallest in its column and largest in its row. Maybe you only want to look for the second kind in which case you can modify the approach accordingly.
Also this code is quite inefficient. You could further optimize it by finding and saving the column maximums, column minimums, row maximums and row minimums before entering the loop.
You could also probably vectorize this further and not use a loop at all, but I think you wanted to see how the basic syntax would look.
% make a matrix to try algorithm on
% there are saddle points at 2,2 and 4,4
A = [10 12 7 3 12;
3 10 6 2 8;
12 24 17 6 10;
15 21 10 8 12;
1 18 22 4 15];
% get dimensions of the matrix
[numRows,numCols] = size(A);
% preallocate array to hold indices for saddle points, there can be at most two
indices = zeros(2,2);
% loop through rows and columns of matrix
numSaddle = 0; % counter
for iRow = 1:numRows
for jCol = 1:numCols
% check if it is the biggest element in its row and smallest
Hello, i think something like this should do nicely:
% To check which element is the smallest in its column, and biggest in its row, for a given matrix say,
% "b", you can first pre-allocate a matrix of zeros where the valid saddle points will be input.
indices=zeros(size(b)); % pre-alocating "indices" based on size of assumed matrix "b"
% Next determine the minimum values of each column of the matrix, "b"
% Determine the maximum values for each row of "b" by first taking the transpose of "b"
% Check for membership of "b_colmin" in "b_rowmax"."lm" is a vector of lowest indices in "b_rowmax" for each value of "b_colmin" in "b_rowmax"
% find the column indices of non-zero indices ("nzCol") in "lm", and the corresponding vector on non-zero values ("nzVec"). The vector "nzVec" in actual sense will be a vector of row indices for the saddle points.
% Input saddle points into marix "indices" based on indices