フィルターのクリア

average of matrix cells

2 ビュー (過去 30 日間)
Ham Man
Ham Man 2023 年 7 月 27 日
回答済み: Voss 2023 年 7 月 28 日
what would be an efficient way to calculate the average of cells 1, cells2 , cells 3 from a matrix?
  3 件のコメント
Ham Man
Ham Man 2023 年 7 月 28 日
編集済み: Ham Man 2023 年 7 月 28 日
Just three numbers which are the average of cells 1,average of cells 2,average of cells 3
Voss
Voss 2023 年 7 月 28 日
By "cells 1", do you mean the elements that are 1? If so, their average will be 1.
Or by "cells 1", do you mean the elements around the outer edges of the matrix, which are labeled as 1 but actually contain other numbers?
I think a concrete example matrix and expected result would be very useful.

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

採用された回答

Voss
Voss 2023 年 7 月 28 日
Here are a couple of guesses:
1.
M = ones(7);
M(2:end-1,2:end-1) = 2;
M(3:end-2,3:end-2) = 3;
M(4,4) = 4
M = 7×7
1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 2 3 3 3 2 1 1 2 3 4 3 2 1 1 2 3 3 3 2 1 1 2 2 2 2 2 1 1 1 1 1 1 1 1
cells1avg = mean(M(M == 1))
cells1avg = 1
cells2avg = mean(M(M == 2))
cells2avg = 2
cells3avg = mean(M(M == 3))
cells3avg = 3
2.
labels = M
labels = 7×7
1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 2 3 3 3 2 1 1 2 3 4 3 2 1 1 2 3 3 3 2 1 1 2 2 2 2 2 1 1 1 1 1 1 1 1
M = randi(10,7,7)
M = 7×7
4 4 7 1 1 6 9 4 10 2 1 5 1 6 4 10 2 8 6 7 5 1 5 2 10 6 8 9 4 8 10 3 3 9 8 5 6 5 9 10 8 10 10 3 6 7 10 6 4
cells1avg = mean(M(labels == 1))
cells1avg = 5.5833
cells2avg = mean(M(labels == 2))
cells2avg = 6.5000
cells3avg = mean(M(labels == 3))
cells3avg = 5

その他の回答 (1 件)

Shubham
Shubham 2023 年 7 月 28 日
Hi Ham,
To calculate the average of specific cells in a matrix in MATLAB, you can use the mean function along with indexing. Here's an example of how you can calculate the average of cells 1, 2, and 3 from a matrix:
% Create a sample matrix
matrix = [1 2 3; 4 5 6; 7 8 9];
% Calculate the average of cells 1, 2, and 3
average = mean(matrix(1:3));
% Display the result
disp(average);
In this example, the matrix is defined as a 3x3 matrix. By using indexing matrix(1:3), we select the first three elements from the matrix. Finally, the mean function calculates the average of those elements, and the result is displayed using disp.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by