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

조회 수: 3 (최근 30일)
studentambitious
studentambitious 2017년 2월 20일
편집: soheilso 2018년 5월 4일
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