how to select two arrays from a group of four
    6 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Lachlan Pemberton-Pigott
 le 13 Nov 2019
  
    
    
    
    
    Commenté : Lachlan Pemberton-Pigott
 le 14 Nov 2019
            i have 4 arrays and i need to square each element then find the total. then the 2 arrays that have to lowest total get selected and assigned a value eg, u3
would love some help from a true matlab master
0 commentaires
Réponse acceptée
  Thomas Satterly
      
 le 13 Nov 2019
        To find the sum square of an array: 
sumSquared = sum(array.^2);
If you have your arrays all together in a matrix:
% If each array is a colum of the matrix
sumSquaredEachArray = sum(matrix.^2, 1);
% If each array is a row of the matrix
sumSquaredEachArray = sum(matrix.^2, 2);
For finding the two lowest scores, let's assume that you have the scores in an array called "sumSquaredEachArray" (which would happen automatically if you have your arrays organized in a matrix)
% Get the order of lower to highest scores
[~, order] = sort(sumSquaredEachArray);
% Select the first two scores from the sorted order, which would be the lowest two
lowestTwoScores = order(1:2);
% Set those scores to "u3"
sumSquaredEachArrau(lowestScores) = u3;
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!