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

3 visualizaciones (últimos 30 días)
studentambitious
studentambitious el 20 de Feb. de 2017
Editada: soheilso el 4 de Mayo de 2018
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 comentarios
Walter Roberson
Walter Roberson el 20 de Feb. de 2017
This sounds like "make work" for a homework assignment, rather than something that would have real use??
studentambitious
studentambitious el 20 de Feb. de 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

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 20 de Feb. de 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 comentario
studentambitious
studentambitious el 20 de Feb. de 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.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 20 de Feb. de 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 el 4 de Mayo de 2018
Editada: soheilso el 4 de Mayo de 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.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by