how can I generate a zero one matrix using mulitiply?
조회 수: 8 (최근 30일)
이전 댓글 표시
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
답변 (5개)
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)
댓글 수: 0
Firouz
2014년 11월 3일
편집: Firouz
2014년 11월 3일
댓글 수: 1
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.
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
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!