# Calculation using matrix indexing for elements with a certain thresholds

15 visualizzazioni (ultimi 30 giorni)
Leon il 20 Ago 2021
Commentato: Leon il 21 Ago 2021
I need to calculate the percentage differences of all elements that are larger than 5 in two matrices (A and B). Both A and B have a size of 1000 x 500. I want my results C to be in the same size of 1000 x 500 as well, so that I could plot them on a contour.
Below are my code:
Ind = A>5 & B>5;
C = (A(Ind)-B(Ind))./B(Ind)*100;
Here is the problem. Even though both my matrix "A" and the index variable "Ind" have a size of 1000 x 500, A(Ind) becomes a column data. Therefore my C is a column data, instead of retaining its original set up of 1000 x 500. In this case, how could I do the calculation to ensure my results will be on the grid and with a size of 1000 x 500?
Many thanks.
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Sulaymon Eshkabilov il 20 Ago 2021
Modificato: Sulaymon Eshkabilov il 20 Ago 2021
There is a couple of small errs in your code and here is a corrected code:
...
A2 = (A>5 & B>5).*A; % Picks up all greater than 5 elements of A and preserves
B2 = (B>5 & B>5).*B; % Picks up all greater than 5 elements of B and preserves
C = (A2-B2)./B2*100;
OPTIONAL: Now you may get rid off or substitute NANs, with 0:
C(isnan(C))=0;
##### 1 CommentoMostra NessunoNascondi Nessuno
Leon il 21 Ago 2021
It works. Many thanks!

Accedi per commentare.

### Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by