Comparing Values in 3D Matrix

2 ビュー (過去 30 日間)
Matthew
Matthew 2016 年 2 月 8 日
コメント済み: Matthew 2016 年 2 月 20 日
I am doing a convergence study, which means I will loop through some numerics and compare the results of t and t-1, until an acceptable convergence value is reached. I intend to construct a 3D matrix (50x50x2). The two planes of the matrix will store t and t-1 values respectively. For each of the 50x50 matrix entries, I need to do an error check between the two planes (i.e., subtract t-(t-1) & take their absolute value). Once all of the 50x50 matrix entries meet the error check and are below a threshold value, convergence is met. I can brute force this and write the code to go through the two planes of the matrix one by one, comparing the 2500 values of t and t-1 each separately. But I have to imagine Matlab has some function to do this automatically without brute force, which would automatically look at the two planes and scour through all of the values looking for exceedances. Does anybody know if such a function exists?

採用された回答

Stephen23
Stephen23 2016 年 2 月 8 日
編集済み: Stephen23 2016 年 2 月 8 日
Just use diff with its optional dim argument:
Y = abs(diff(X,1,3))
You can then compare all of these values with a scalar threshold:
idx = Y>thr
what you do with the index depends on your code. To check that none of the value are above the threshold, you just need to do this:
~any(idx)
  3 件のコメント
Stephen23
Stephen23 2016 年 2 月 18 日
編集済み: Stephen23 2016 年 2 月 18 日
The tilde ~ is a logical NOT of each element in the array it is operating on. You can try it yourself:
>> [1,0,1]
ans = 1 0 1
>> ~[1,0,1]
ans = 0 1 0
You might also like to experiment with this:
~any(idx(:))
Matthew
Matthew 2016 年 2 月 20 日
Thank you for the help. I was able to write my code using your suggestions!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by