How to use the sum command to get both the sum and output in the same array
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Jay
      
 el 15 de Jul. de 2019
  
    
    
    
    
    Comentada: Jay
      
 el 15 de Jul. de 2019
            Using the following code as an example, I would like to get the sum of each row (columns 1:3) and output the rows summation in the same array in column 4.
Is there a way to use the sum command (in-built function) or do I have to have a "for" sub-routine to calculate the sum iteratively?
clear
clc
% How to get the 4th col to have summation of cols 1:3?
%% Input
% Array initalization
samp(10,4) = zeros
% Populate with random integer values
samp(:,1:3) = randi([1,15],10,3)
0 comentarios
Respuesta aceptada
  Image Analyst
      
      
 el 15 de Jul. de 2019
        Try this:
clc
% How to get the 4th col to have summation of cols 1:3?
%% Input
% Array initalization
samp(10,4) = zeros
% Populate with random integer values
samp(:,1:3) = randi([1,15],10,3)
samp(:, end) = sum(samp, 2)  % <===== Append this line to your existing code.
Más respuestas (0)
Ver también
Categorías
				Más información sobre Matrices and Arrays en Help Center y File Exchange.
			
	Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

