필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
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
Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 4일
The explanation is not clear
Paul Sponagle
Paul Sponagle 2016년 8월 4일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 4일
C=B
C(~any(A,2),:)=0

추가 답변 (1개)

Paul Sponagle
Paul Sponagle 2016년 8월 4일
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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by