How to replace specific values with 0's
1 ビュー (過去 30 日間)
表示 古いコメント
Dear all,
I have a 1435x205 matrix and I want to obtain the sum of a specific set of cells. In this case it is from the 146th to the 150th column and from the 316th to 351th row, which gives me a 35x5.
My first thought was to replace all values in the matrix except the specific values I just pointed out with 0's and then use a summation vector. Does anyone have an idea on how to do this? Preferably a generalized method.
For example assume that I have a 5x5 matrix
A = magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
And let's assume I want the values in the 2nd and 3rd row and the second and third row to be replaced by 0. Which gives me this result:
A =
0 0 0 0 0
0 5 7 0 0
0 6 13 0 0
0 0 0 0 0
0 0 0 0 0
Is there a simple command for this?
Finally, I want to sum these values (the total is 31):
sum=ones(1,5)*A*ones(5,1)
sum =
31
If anyone knows, is there a more succinct way to pick out which elements from a matrix matlabs sums using summation vectors?
Best wishes.
採用された回答
Voss
2022 年 1 月 12 日
A = rand(1435,205);
B = A(316:351,146:150) % 36-by-5
sum(B(:))
or:
sum(sum(A(316:351,146:150)))
or:
sum(A(316:351,146:150),'all')
0 件のコメント
その他の回答 (1 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!