How can I convert double values into integers for indices to create a sparse matrix in MATLAB?

19,933

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.

Share:
19,933

Related videos on Youtube

Vass
Author by

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, 2022

Comments

  • Vass
    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 using uint8, int32 and int64 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
    Vass about 11 years
    when 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
    Vass about 11 years
    I did ` class(ceil(col1(:))) ans =double` and class(round(col1(:)))ans =double
  • Eitan T
    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
    Vass about 11 years
    thanks 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
    Shai about 11 years
    To 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 ); with doubles
  • Vass
    Vass about 11 years
    I 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
    Shai about 11 years
    is it possible that listedges has elements smaller than 1? elements that when rounded are zero or negative?
  • Vass
    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
    Vass about 11 years
    apologies, but I had a NaN in the file