フィルターのクリア

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

1 回表示 (過去 30 日間)
i want to plus all my numbers in my array of 3x3 then divide it by its area using for loop function and save please

採用された回答

fatma zahra
fatma zahra 2024 年 5 月 23 日
編集済み: fatma zahra 2024 年 5 月 23 日
% 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)]);
The result is: 5
  3 件のコメント
fatma zahra
fatma zahra 2024 年 5 月 23 日
hey, I think it's correct ( 45 /9 =5) based on this example
Rozh
Rozh 2024 年 5 月 25 日
thank you soo much you saved me

サインインしてコメントする。

その他の回答 (2 件)

the cyclist
the cyclist 2024 年 5 月 23 日
You can sum all the elements of an array using the sum function. For example
A = magic(3)
A = 3x3
8 1 6 3 5 7 4 9 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S = sum(A,"all")
S = 45
You can save to file using the save function.
I don't know what you mean by the "area" of an array.
  2 件のコメント
Rozh
Rozh 2024 年 5 月 23 日
for example if my array is 3x3 i divide it by 9 ...thank you
the cyclist
the cyclist 2024 年 5 月 23 日
You can use numel to get the number of elements.

サインインしてコメントする。


Kunal Kandhari
Kunal Kandhari 2024 年 5 月 23 日
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:
Furthermore, you can learn the basics of MATLAB through this introductory tutorial on commonly used features and workflow:
  2 件のコメント
Rozh
Rozh 2024 年 5 月 23 日
i just need to do that one specific thing...its a homework...i need to create an array of 3x3 and plus all and divide the number by the area like (if my array is 4x4 i divide by 16) using function and for loops
Image Analyst
Image Analyst 2024 年 5 月 26 日
@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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by