how can I generate a zero one matrix using mulitiply?
6 views (last 30 days)
Show older comments
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
0 Comments
Answers (5)
Image Analyst
on 3 Nov 2014
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)
0 Comments
Firouz
on 3 Nov 2014
Edited: Firouz
on 3 Nov 2014
1 Comment
Image Analyst
on 3 Nov 2014
Edited: Image Analyst
on 3 Nov 2014
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.
Image Analyst
on 11 Nov 2014
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
Image Analyst
on 11 Nov 2014
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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!