What are the ways to sum matrix elements in MATLAB?

前端 未结 6 2066
天涯浪人
天涯浪人 2021-02-08 01:26

Given the matrix:

A = [1 2 3; 4 5 6; 7 8 9];
  1. How could you use a for loop to compute the sum of the elements in the matrix?
  2. Writ
6条回答
  •  抹茶落季
    2021-02-08 01:53

    Another answer for the first question is to use one for loop and perform linear indexing into the array using the function NUMEL to get the total number of elements:

    total = 0;
    for i = 1:numel(A)
      total = total+A(i);
    end
    

提交回复
热议问题