How can I convert double values into integers for indices to create a sparse matrix in MATLAB?
Solution 1
There is no need for any conversion, keep the indices double
:
r = round(listedges);
graph = sparse(r(:, 1), r(:, 2), ones(size(listedges, 1), 1));
Solution 2
There are two reasons why one might want to convert to int
:
The first, because you have data type restrictions.
The second, your inputs may contain fractions and are un-fit to be used as integers.
If you want to convert because of the first reason - then there's no need to: Matlab works with double
type by default and often treats doubles
as ints (for example, when used as indices).
However, if you want to convert to integers becuase of the second reason (numbers may be fractionals), then you should use round()
, ceil()
or floor()
- whatever suits your purpose best.
Related videos on Youtube
Vass
This is the third phase of my StackOverflow/Stackexchange experience. Maths/Stats and the IT means to do it! (does AI fit into it or does it revolve around it? or... do they revolve around I?)
Updated on June 04, 2022Comments
-
Vass almost 2 years
I am using MATLAB to load a text file that I want to make a sparse matrix out of. The columns in the text file refer to the row indices and are
double
type. I need them to be integers to be able to use them as indices for rows and columns. I tried usinguint8
,int32
andint64
to convert them to integers to use them to build a sparse matrix as so:??? Undefined function or method 'sparse' for input arguments of type 'int64'. Error in ==> make_network at 5
graph =sparse(int64(listedges(:,1)),int64(listedges(:,2)),ones(size(listedges,1),1));
How can I convert the text file entries loaded as double so as to be used by the
sparse
function? -
Vass about 11 yearswhen I do that I get the error
??? Error using ==> sparse Index into matrix must be an integer. Error in ==> make_network at 6 graph = sparse(listedges(:, 1), listedges(:, 2), ones(size(listedges, 1), 1));
-
Vass about 11 yearsI did ` class(ceil(col1(:))) ans =double` and
class(round(col1(:)))ans =double
-
Eitan T about 11 years@Vass Then that's probably because listedges is not a round number (an integer). I've amended the answer.
-
Vass about 11 yearsthanks for this but I still get the error:
??? Error using ==> sparse Index into matrix must be an integer. Error in ==> make_network at 8 graph = sparse(r(:, 1), r(:, 2), ones(size(listedges, 1), 1));
-
Shai about 11 yearsTo construct a sparse matrix matlab expect the indices to be integer numbers of type double. So, you can call
sparse( round( listedges(:,1) ), round( listedges(:,2) ), 1 );
withdouble
s -
Vass about 11 yearsI still get the error
??? Error using ==> sparse Index into matrix must be an integer. Error in ==> make_network at 9 graph=sparse( round( listedges(:,1) ), round( listedges(:,2)), 1 );
-
Shai about 11 yearsis it possible that
listedges
has elements smaller than 1? elements that whenround
ed are zero or negative? -
Vass about 11 years
K>>min(listedges(:,1))ans=1 K>> min(listedges(:,2)) ans=1
, I suspected that too but not there is nothing lower than 1. -
Vass about 11 yearsapologies, but I had a NaN in the file