# Indexing a matrix in matlab according to conditions set on other matrices

4 visualizzazioni (ultimi 30 giorni)
Nikit Parakh il 21 Giu 2020
Commentato: Nikit Parakh il 21 Giu 2020
I am trying to index my matrix based on two conditions, I'll explain.
Let's say I have two matrices:
a = [7 3 4; 5 6 7; 4 8 0];
b = [1 9 8; 2 4 6; 6 1 6];
And a third matrix to index:
c = [1 2 3; 4 5 6; 7 8 9];
My aim is to index c in a way that I get a 3x3 matrix in which only the values of c are copied over for whose indexes the following conditions are met and the rest are zeros.
a <= 5, b >= 6
Resulting matrix:
result = [0 2 3; 0 0 0; 7 0 9]
I hope I was able to explain my problem.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Tommy il 21 Giu 2020
Here's one way:
result = (a <= 5 & b >= 6) .* c;
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Nikit Parakh il 21 Giu 2020
Thanks!

Accedi per commentare.

### Categorie

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

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by