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

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2017-2-20
This sounds like "make work" for a homework assignment, rather than something that would have real use??
studentambitious
studentambitious 2017-2-20
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

请先登录,再进行评论。

回答(3 个)

Image Analyst
Image Analyst 2017-2-20
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 个评论
studentambitious
studentambitious 2017-2-20
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.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-2-20
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 2018-5-4
编辑:soheilso 2018-5-4
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.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by