필터 지우기
필터 지우기

How do I make the element zero, if its adjacent column element is zero?

조회 수: 1 (최근 30일)
Hello,
Assume, I have the following 9x2 matrix. When there is a zero in the second column, I want to make the first column's same row element zero too.
12 0
11 0
10 6
12 0
11 3
10 0
12 0
11 5
10 0
This will become like:
0 0
0 0
10 6
0 0
11 3
0 0
0 0
11 5
0 0
Thank you so much

채택된 답변

per isakson
per isakson 2016년 7월 10일
편집: per isakson 2016년 7월 10일
Try
>> is_zero = A(:,2)==0;
>> A(is_zero,1)=0;
>> A
A =
0 0
0 0
10 6
0 0
11 3
0 0
0 0
11 5
0 0
  댓글 수: 1
Taner Cokyasar
Taner Cokyasar 2016년 7월 10일
Thank you so much for both of your comments. I was thinking that using 'if' were the only way of doing it.

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

추가 답변 (1개)

Star Strider
Star Strider 2016년 7월 10일
That can actually be done in one line of code:
M = [12 0
11 0
10 6
12 0
11 3
10 0
12 0
11 5
10 0];
M(M(:,2) == 0,1) = 0
M =
0 0
0 0
10 6
0 0
11 3
0 0
0 0
11 5
0 0
  댓글 수: 3
Taner Cokyasar
Taner Cokyasar 2016년 7월 11일
Can you help me also on this:
I have the following sample vector named Zvalues:
Variable Name(Zim) Zvalues
Z11 0
Z12 0
Z13 1
Z21 0
Z22 1
Z23 0
Z31 0
Z32 1
Z33 0
I have created a zero 3x9 sized matrix as following:
Y= zeros(3, 9);
I want to fill this matrix with an if condition, as the following example:
if Zim=1, Yim=1
Y13 = 1 because Z13 = 1
Y22 = 1 because Z22 = 1
Y32 = 1 because Z32 = 1
The following matrix is what I want to reach at the end. 'Y's represent variable names, they are not a part of the matrix .
Y11 Y12 Y13 Y21 Y22 Y23 Y31 Y32 Y33
0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1 0
In other words, I want to formulate an Aeq matrix for coefficients of Y(im) variables given in the following equality constraint (to use with intlinprog):
Y(im)=t(m)-Z(im)*f(i) %construct this constraint for variables if Z(im)=1 (Zim is same as Zvalues)
%There is nothing to do with the RHS of the equality. It is just given to make the question more clear.
As seen in the equation, coefficients of all Y(im) variables are 1. In my sample I have 9 Y(im) variables, since i=3 and m=3. I will have larger dimensions. So, I need a MATLAB formulation for this mathematical form.
Thanks for help.
per isakson
per isakson 2016년 7월 12일
편집: per isakson 2016년 7월 12일
Adding a question in a comment to an existing question with an accepted answer doesn't attract many viewers. I think it is better to open a new question.
I have problems understanding your "pseudo indexing".
"Y13 = 1 because Z13 = 1" &nbsp Here is a piece of elegant Matlab code. (Not sure it is relevant, though.)
>> A = zeros(3,4,2);
>> B = randi( [1,2], size(A) );
>> A(B==1)=1; % or A(B==1)=B(B==1);
>> B
B(:,:,1) =
2 2 1 2
1 1 1 2
2 2 2 1
B(:,:,2) =
2 1 1 2
1 1 2 1
1 2 1 2
>> A
A(:,:,1) =
0 0 1 0
1 1 1 0
0 0 0 1
A(:,:,2) =
0 1 1 0
1 1 0 1
1 0 1 0
>>

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by