# create a new matrix with combining two matrices with the same size

1 visualizzazione (ultimi 30 giorni)
Maryam Hamrahi il 4 Ago 2016
Risposto: Paul Sponagle il 4 Ago 2016
My aim is to create a new matrix which the same rows in matrix A and matrix B are zero. the same rows in matrix A which are zero, should also be zero in matrix C.
I want to create matrix C from matrix B.
A=
1 2
0 0
3 2
0 0
B=
7 2
3 6
4 5
2 6
C=
7 2
0 0
4 5
0 0
##### 2 CommentiMostra NessunoNascondi Nessuno
Azzi Abdelmalek il 4 Ago 2016
The explanation is not clear
Paul Sponagle il 4 Ago 2016
Is this what you are trying to do?
A=[1,2;0,0,;3,2;0,0]
B=[7,2;3,6;4,5;2,6]
C=B;
C(A==0)=0
Results in:
C =
7 2
0 0
4 5
0 0
This will work for all elements of A that are zero. If you only want to modify B if both elements in the row of A are zero this won't work.
Rather than guessing your intent I provided the simpler solution.

Accedi per commentare.

### Risposta accettata

Azzi Abdelmalek il 4 Ago 2016
C=B
C(~any(A,2),:)=0
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Maryam Hamrahi il 4 Ago 2016
Thanks a lot Azzi Abdelmalek.

Accedi per commentare.

### Più risposte (1)

Paul Sponagle il 4 Ago 2016
I put this in comments, it should have been in the 'Answers' section.
The question was not quite clear, but I think this might also work:
A=[1,2;0,0,;3,2;0,0]
B=[7,2;3,6;4,5;2,6]
C=B;
C(A==0)=0
Results in:
C =
7 2
0 0
4 5
0 0
This will create C by copying and modifying B for all elements of A that are zero. If you only want to modify B if both elements in the row of A are zero this won't work and you need to see Aziz's answer.
Paul
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by