フィルターのクリア

How to sum of the values of z corresponding to the ranges of x and y?

1 回表示 (過去 30 日間)
SOM
SOM 2023 年 3 月 20 日
コメント済み: Rik 2023 年 3 月 22 日
Hello,
I have a 3D matrix (x, y ,z) such as:
[1 -5 -2; 0 2 3; 2 -5 -2; -0.5 2 5; -0.8 3 3.5]
Can we find the sum of the values of z corresponding to the ranges of x and y in x, y and z?
For simple example, the sum of z values corresponding to -1<x<1 and 1<y<5.
Any suggestions?
Thank you!

回答 (1 件)

Rik
Rik 2023 年 3 月 20 日
If your ranges grow more complex, you should try to define groups so you can use splitapply. But for this simple example you can simple use logical indexing on your 2D (not 3D) data.
data = [1 -5 -2; 0 2 3; 2 -5 -2; -0.5 2 5; -0.8 3 3.5];
x = data(:,1);
y = data(:,2);
z = data(:,3);
value = sum(z( x>=-1 & x<1 & y>=1 & y<5 ));
disp(value)
11.5000
  2 件のコメント
SOM
SOM 2023 年 3 月 22 日
移動済み: Rik 2023 年 3 月 22 日
Thank you !! It was a great help
Rik
Rik 2023 年 3 月 22 日
You're welcome. If I solved your question, please conder marking it as accepted answer.

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

カテゴリ

Help Center および File ExchangeMathematics and Optimization についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by