# i need to plus all numbers in my Array of 3x3 then divide that number by its area using save ,for loop, function

1 view (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

fatma zahra
on 23 May 2024

Edited: fatma zahra
on 23 May 2024

% Define the 3x3 array

array = [1, 2, 3; 4, 5, 6; 7, 8, 9];

% Initialize sum variable

totalSum = 0;

% Calculate the sum of all elements using a for loop

for i = 1:3

for j = 1:3

totalSum = totalSum + array(i, j);

end

end

% Calculate the area of the array (number of elements)

area = numel(array);

% Divide the sum by the area

result = totalSum / area;

% Display the result

disp(['The result is: ', num2str(result)]);

##### 3 Comments

### More Answers (2)

the cyclist
on 23 May 2024

You can sum all the elements of an array using the sum function. For example

A = magic(3)

S = sum(A,"all")

I don't know what you mean by the "area" of an array.

##### 2 Comments

Kunal Kandhari
on 23 May 2024

To understand how to sum elements in an array, use for loops, and perform basic arithmetic operations in MATLAB, you can refer to the following resources:

MATLAB Documentation:

- Array Indexing: This documentation explains how to access and manipulate elements in arrays. You can find it here: https://www.mathworks.com/help/matlab/math/array-indexing.html
- For Loops: Learn about the syntax and use of for loops in MATLAB. Check it out here: https://www.mathworks.com/help/matlab/ref/for.html
- Sum of Array Elements: This explains different methods to sum elements in an array. The documentation is available here: https://www.mathworks.com/help/matlab/ref/sum.html

Furthermore, you can learn the basics of MATLAB through this introductory tutorial on commonly used features and workflow:

- MATLAB Onramp: A free, self-paced introductory tutorial provided by MathWorks. It covers the basics of MATLAB, including array operations and loops. Start learning here: https://www.mathworks.com/learn/tutorials/matlab-onramp.html

##### 2 Comments

Image Analyst
on 26 May 2024

@Rozh You should have said that it was your homework before, and tagged it as homework, which I just did for you.

I hope you haven't already turned in @fatma zahra's solution as your own or you may get in trouble for cheating. You're supposed to turn in your own work, not someone else's and claim it as your own. Some universities use plagiarism detectors to check for things like this. Good luck.

Also, @fatma zahra should not have used area as the name of the variable since it's the name of a built-in function

### See Also

### Community Treasure Hunt

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

Start Hunting!