Help with indexing problem.
    2 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    ajk1
 el 29 de Abr. de 2015
  
    
    
    
    
    Comentada: ajk1
 el 29 de Abr. de 2015
            Hi, I would like help with this indexing problem. I have a 5x3 matrix called 'P_table' and I want to find the sum for all the rows and subtract the maximum value of each row. Also I would like to know how to use the index to check which column the maximum value is for each row in a variable 'column', if a specific column does not contain any maximum value then all the elements in that column are zeroed. Thanks.
for ite=1:length(P_table)
P_max = find(max(P_table(ite,:),[],2));
P_max = P_table(P_max); 
P_sum=sum(P_table,2)-P_max; 
end
For example if
P_table=
      0.2432   0.3687   0.2879   
      0.2225   0.5833   0.3497   
      0.2485   0.4484   0.4167   
      0.2859   0.2019   0.2720   
      1.0000   0.6313   0.4401
Then column=
      2   
      2     
      2     
      1  
      1
Since column 3 does not contain any of the maximum values it is zeroed.
P_table=
      0.2432   0.3687   0   
      0.2225   0.5833   0   
      0.2485   0.4484   0   
      0.2859   0.2019   0   
      1.0000   0.6313   0
P_sum=
      0.2432   
      0.2225     
      0.2485     
      0.2019  
      0.6313
0 comentarios
Respuesta aceptada
  Image Analyst
      
      
 el 29 de Abr. de 2015
        Instead of a for loop, use the appropriate input and output arguments of sum() and max():
P_table=[...
      0.2432   0.3687   0.2879   
      0.2225   0.3497   0.5833   
      0.2485   0.4484   0.4167   
      0.2859   0.2019   0.2720   
      1.0000   0.6313   0.4401]
% Vectorized approach:
% Find sums of rows, going across all columns.
rowSums = sum(P_table, 2)
% Find maximas in each row and the column where it occurs.
[rowMaxima, columsOfMaxima] = max(P_table, [], 2)
% Compute the difference.
P_sum = rowSums - rowMaxima
It gives you exactly what you showed.
3 comentarios
  Image Analyst
      
      
 el 29 de Abr. de 2015
				New code to delete columns with no max in them:
P_table=[...
      0.2432   0.3687   0.2879   
      0.2225   0.3497   0.5833   
      0.2485   0.4484   0.4167   
      0.2859   0.2019   0.2720   
      1.0000   0.6313   0.4401]
% Vectorized approach:
% Find sums of rows, going across all columns.
rowSums = sum(P_table, 2)
% Find maximas in each row and the column where it occurs.
[rowMaxima, columsOfMaxima] = max(P_table, [], 2)
% Compute the difference.
P_sum = rowSums - rowMaxima
% Get rid of any columns that don't contain at least one maximum
[rows, columns] = size(P_table)
columnsToDelete = setdiff(1:columns, unique(columsOfMaxima))
if ~isempty(columnsToDelete)
  % Delete those columns
  P_table(:, columnsToDelete) = [];
end
If this answers the question, can you mark it as "Accepted"?
Más respuestas (0)
Ver también
Categorías
				Más información sobre Startup and Shutdown 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!

