how can I generate a zero one matrix using mulitiply?

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

Answers (5)

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))
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 on 3 Nov 2014
Edited: Firouz on 3 Nov 2014
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 Comment

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.

Sign in to comment.

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 Comments

Firouz commented
Thank you Matt J, Your code doesn't work! my problem hasn't been solved! I appreciate it if anyone can help me. Thanks
Worked fine for me, as you can see from my example above. You will find you get better help in the forum if you give more detailed info than "it doesn't work".
Firouz commented
Dear Matt, I see this error when I run your code:
classdef MatrixObj
|
Error: Illegal use of reserved keyword "classdef".
Make sure you've followed the instructions in InstallationGuide.txt. You cannot unzip the files and put them wherever you want. You must also ensure that the the parent directory of @MatrixObj/ is on the MATLAB path but @MatrixObj/ itself is NOT on the path.
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
modify a to
a.Ops.mtimes=@(p,q) nnz(q);

Sign in to comment.

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 Comments

Thank You, It's not what I want and as I said before I can't use nnz() because I don't have the n (or X) matrix now! As I said, I don't have the n matrix at the moment because it is the solution of my equation:
max C*X
subject to:
A*X <= B
I don't have the X matrix at the moment and I can't use any function about X because it will be available after solving the problem in the solver software (CPLEX), so I need to define A matrix in which get that kind of results which I said in the previous comment.
Thanks
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.
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

Sign in to comment.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Asked:

on 3 Nov 2014

Commented:

on 11 Nov 2014

Community Treasure Hunt

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

Start Hunting!