how to search a NXN matrix from the smallest entry to the largest, and checking to see if row = column or if the column does not equal to.

2 views (last 30 days)
i have a 50X50 matrix. I am trying to go through the matrix incrementing from smallest value to the largest. when even the an row number equal column number i increment a count, and when the columns and row are not equal i increment another count.
  6 Comments
pj brown
pj brown on 21 Sep 2014
% this is what i have
% if true
max = max(A);
min = min(A);
thresold = .03;
[row,column] = size(A);
counter1 = 0;
counter2 = 0;
for min : thresold : max
for rows = 1:columns
if rows == columns
counter1 = counter1 + 1;
end
end
end
Image Analyst
Image Analyst on 21 Sep 2014
First of all, don't use min and max for variable names since they are built in functions. Secondly, your outer for loop is not even constructed properly - what is the loop iterator? Third, columns is not defined anywhere. Fourth, you get row and column but never use them anywhere. Finally, you never do anything with counter2 except set it to zero before the loop starts.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 21 Sep 2014
If you're looking to count the number of elements where its row number equals its column number (which will happen only along the main diagonal), I can tell you right now that the first count will be 50, and the second count will be 50*50-50 = 2450. No sorting or scanning of the array needed.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!