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
回答 (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))
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)
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.
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 件のコメント
Matt J
2014 年 11 月 4 日
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
Matt J
2014 年 11 月 5 日
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.
Firouz
2014 年 11 月 11 日
Matt J
2014 年 11 月 11 日
modify a to
a.Ops.mtimes=@(p,q) nnz(q);
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 件のコメント
Firouz
2014 年 11 月 11 日
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
2014 年 11 月 11 日
カテゴリ
ヘルプ センター および File Exchange で Parallel Computing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!