简体   繁体   中英

How to convert logical sparse matrix into integer sparse matrix in MATLAB?

I have a matrix https://www.cise.ufl.edu/research/sparse/matrices/Hamm/add20.html I want to consider it as adjacency matrix of corresponding graph, so I'm replacing every non zero element with 1:

A = A ~- 0

Now I want to calculate A*A but I can't because

>> A*A    
Error using *
Both logical inputs must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead

and I for some reason I cannot just convert logical matrix to integer

>> uint(A)
Error using numerictype (line 172)
Invalid arguments (WordLength must be a scalar numeric value).

Error in fixdt (line 186)
        embeddedType = numerictype( varargin{:} );

Error in uint (line 14)
DataType = fixdt(0,WordLength,0);

I could do it by converting matrix to full rank back and forth but this is impractical for my task.

Firstly, you would want to be using uint8 or similar, as opposed to uint - read the help files as to what the difference is, uint doesn't do what you think it does. However, according to this forum post, the only valid sparse data types are double or logical . You've got a logical matrix, but it would appear that sparse matrix multiplication is not defined for logical matrices. Thus, you must convert A to double form before you will be able to multiply it as you are trying to.

Alternatively, use A^2 instead - this will work with logical sparse matrices. Don't know why it's different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM