how to find range of elements in each row of a matrix

2 views (last 30 days)
i have a matrix of 8 x 5 and i want if there are at least two values in a row that lies between 6 and 8,then store the locations of those value else decrease the lower value from 6 to 5 and check if any value lies between 5 &8 else increase the upper value to 9 and check the values of a row lies between 7&9...I need to set the the range for each row and to store the location of the values in each row that lie in a specified range
  2 Comments
Walter Roberson
Walter Roberson on 20 Feb 2017
This sounds like "make work" for a homework assignment, rather than something that would have real use??
studentambitious
studentambitious on 20 Feb 2017
i m doing project where i need this thing to be done.. actually i have done a program where i have checked if the values lie between 7 & 8 and also find the locations of those values but i m not able to find the solution how to move the loop to increase and decrease the range for each row. shall i use switch/case or something else

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 20 Feb 2017
If you want the range, why do it like that unusual way, which won't necessarily find the true range? Why not simply do this:
m = 9 * rand(5, 8) % Sample data
for row = 1 : 5
[minValue(row), colOfMin(row)] = min(m(row,:));
[maxValue(row), colOfMax(row)] = max(m(row,:));
end
  1 Comment
studentambitious
studentambitious on 20 Feb 2017
actually range here is used for thresholding.. i need to check in each row if any or at least two values lies within a range of 7 & 8 If no then i have to check the values between range between 6(7-1) and 8 or if not then shift the higher range from 8 to 9.for each row the starting range will remain the same i.e. 7 & 8.

Sign in to comment.


Walter Roberson
Walter Roberson on 20 Feb 2017
Let M be your matrix.
nrow = size(M,1);
location_to_remember = zeros(nrow, 1);
active_rows = 1 : nrow;
for thresh = [6 5 7; 8 8 9]
mask = M(active_rows, 1:end-1) >= thresh(1) & M(active_rows, 1:end-1) <= thresh(2) & ...
M(active_rows, 2:end) >= thresh(1) & M(active_rows, 2:end) <= thresh(2);
rows_with_matches = find( any(mask,2) );
for K = 1 : rows_with_matches
this_relative_row = rows_with_matches(K);
first_matchpair_in_row = find( mask(this_relative_row, :), 1, 'first' );
this_orig_row = active_rows(this_relative_row);
location_to_remember(this_orig_row) = first_matchpair_in_row;
end
active_rows(rows_with_matches) = [];
end
It is possible to vectorize the finding of the match pair instead of using the for K loop, but the code to do so would be less than clear.

soheilso
soheilso on 4 May 2018
Edited: soheilso on 4 May 2018
You are going to find the range of elements in A:
mins=min(A,[],2) %Gives you the minimum in each row.
maxs=max(A,[],2) %Gives you the maximum in each row.

Categories

Find more on Creating and Concatenating Matrices 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!