calculation with if else statement

10 ビュー (過去 30 日間)
kim chan
kim chan 2020 年 4 月 4 日
回答済み: Image Analyst 2020 年 4 月 4 日
Hello,
I have a problem about using if-else statement for the following calculation (given that the size of A is 100*60*1200, 100 and 60 indicate the long and lat; median_A, min_A and max_A also have the same size, i.e.100*60*1200):
anomaly=(median_A - A)/(median_A - min_A)*100 if A median_A
anomaly=(median_A - A)/(max_A - median_A)*100 if A > median_A
I wrote the following simple code but the result looks unrealistic (go beyond the range e.g. larger than 100) because the answer should be ranged between -100 and 100 (I also tried replacing = with ==, it still does not work):
if A <= median_A
anomaly = ((median_A - A)./(median_A - min_A)*100);
else
anomaly = ((median_A - A)./(max_A - median_A)*100);
end
Do any professionals know how to solve this problem? Many appreciations!

採用された回答

Image Analyst
Image Analyst 2020 年 4 月 4 日
You need to create a logical index (a "map") first of where A is less than its median. Then use that logical index to make the assignment
A = rand(10, 60, 1200); % Create sample data.
median_A = median(A(:))
max_A = max(A(:))
min_A = min(A(:))
% Find where A is less than it's median.
logicalIndexes = A <= median_A; % A 3-D logical array.
% First initialize everything:
anomaly = ((median_A - A)./(max_A - median_A)*100);
% Make up second set of numbers.
A2 = ((median_A - A)./(median_A - min_A)*100);
% Then replace only those indexes where A(i,j,k) < median_A
anomaly(logicalIndexes) = A2(logicalIndexes);
Also, look up the mad() and isoutier() functions.

その他の回答 (0 件)

カテゴリ

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