how can I generate a zero one matrix using mulitiply?

조회 수: 8 (최근 30일)
Firouz
Firouz 2014년 11월 3일
댓글: Matt J 2014년 11월 11일
Hi everybody, I am going to create a matrix resulting from multiplying two matrices so that i can count the non zeros, for example: X = [0.1 1 0 0.3 0.004 0]; C = A*X
I need to know how many cells of C are non zero, so I need to define A so that by multiplying each cell with a nonzero number I can get for example 1 and for zeros get zero. I think if I can use a number, a constant or a function in MATLAB that by multiplying it to a zero gives zero and by multiplying to a non-zero gives constant or 1, my problem is solved! I'm doing optimization using CPLEX, the X matrix is unknown and i need to create A matrix to use it as the coefficient matrix.
Thanks

답변 (5개)

Orion
Orion 2014년 11월 3일
편집: Orion 2014년 11월 3일
Hi,
Use logical indexing to get all zeros or non-zeros values of any vector or matrix.
X = [0.1 1 0 0.3 0.004 0];
Xzeros = X==0;
Xnonzeros = X~=0;
ex : disp all non-zeros values :
disp(X(Xnonzeros))

Image Analyst
Image Analyst 2014년 11월 3일
Why not just simply use the function in MATLAB made to do that: nnz()? nnz() counts the number of non-zeros, just like you asked for:
nnz
Number of nonzero matrix elements
Syntax
n = nnz(X)

Firouz
Firouz 2014년 11월 3일
편집: Firouz 2014년 11월 3일
Thank you guys, but it's not my answer! as I said, I don't have the X (which is the solution vector)! if the X was known, it was so easy! I need to define A matrix, let me explain my question from another point of view: how can I have a multiplier in which by multiplying it to a non zero value (a number between 0 and 1), I can get 1 and by multiplying it to zero get zero:
a * x = 0 (if x is zero)
and
a * x = 1 (if x is nonzero)
so, in MTLAB what can I use instead of a?
Thanks
  댓글 수: 1
Image Analyst
Image Analyst 2014년 11월 3일
편집: Image Analyst 2014년 11월 3일
Perhaps a=1/x or a=inv(x) ??? You can't really do much if you don't have x and you don't have a. If you don't know anything, what can you do, unless a is a function like Matt suggests.

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


Matt J
Matt J 2014년 11월 3일
Not sure why you want to do this, but you could use my MatrixObj class ( Download ). So, for the example you describe, you could do
>> a=MatrixObj; a.Ops.mtimes=@(p,q) logical(q);
and now,
>> a*3
ans =
1
>> a*0
ans =
0
  댓글 수: 6
Firouz
Firouz 2014년 11월 11일
Thank you Matt, The "a" matrix works but still I need your help because I need to have the cell by cell multiplier matrix in which the result would be a number and not a vector, for example:
n = [1;0.5;0;0.3;1;0];
I need a matrix so that:
a*n = 4
right now I get
a*n
ans =
1
1
0
1
1
0
which is not my solution!
Thank you again
Matt J
Matt J 2014년 11월 11일
modify a to
a.Ops.mtimes=@(p,q) nnz(q);

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


Image Analyst
Image Analyst 2014년 11월 11일
Firouz, try this to get a*n=4:
n = [1;0.5;0;0.3;1;0]
% Find an a such that a*n = 4
a=1./n' % May have infinities
a(n==0) = 0 % Set infinities = 0
% Now do a matrix multiple of the 1 by 6 "a" times the 6 by 1 "n".
out = a*n % This will equal 4
Note: I still don't know why you don't use nnz() like I originally suggested - it's so much simpler.
  댓글 수: 3
Image Analyst
Image Analyst 2014년 11월 11일
Alright, sorry - I give up. You give an example for n and then say you don't have it or know it. And you say you also don't have "a" or "X". So it seems like you don't have anything whatsoever to start with and are not allowed to specify anything (like n) either. And you " can't use any function ". I don't know what you can specify or do. So I'm totally lost and confused, and I'll just bow out. Good luck with it though.
Firouz
Firouz 2014년 11월 11일
Sorry guys for confusing you, that's my bad.
I need to create a matrix in which, by multiplying each cell of that to a non zero number we get 1 and get zero for zero number:
a = [a1 a2 a3];
X = [x1;x2;x3];
A*X = a1*x1 + a2*x2 + a3*x3
I need the A in which a1*x1 = 1 (if x1 is non-zero) and a1*x1 = (if x1 is zero), and so on. I am allowed to just define A, nothing about X.
Thanks

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

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by