How can I re-create sparse matrixes from the row and column indexes, and values?

4 views (last 30 days)
Hi there,
I have a struct Z, which contain 28 structs(Z1,....,Z28).
Each of them has
i (int32) = whose numbers represent the row indexes
j (int32) = whose numbers represent the columns indexes
x (double) = with values
This data was imported from 28 sparses matrixes (24000x24000), using R, in a way that the length of i,j and x are the same, but x(1,1) does not necessarily populate Z(i1,j1). For example, considering Z1, the first value of x is actually the element z(126,126).
Now, in Matlab, I need to recreate these 28 matrixes (24000x24000) matching the values of x with the i,j indexes.
Any clue on how to solve it. I would be extremely grateful!
Using a loop would be the best!
Cheers,
  1 Comment
Tiago Furlanetto
Tiago Furlanetto on 10 Mar 2022
Thanks foryour message, Torsten.
Z1,..., Z28 are the tow-dimensional matrixes I inported from R.
with z(126,126), I meant that first valeu of x is located in row 126, column 126 of matrix Z1.
Did I explain myself?

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 10 Mar 2022
Let's take a simple example:
rng default
S = sprand(10, 10, 0.1); % 10-by-10 with roughly 10 nonzero elements
Let's get the vectors of row and column indices and values
[rows, cols, values] = find(S);
Recreate S using rows, cols, values. In this case S has elements in both the last row and last column but to handle the general case where it didn't let's also state how large it needs to be.
S2 = sparse(rows, cols, values, 10, 10);
Is S the same as S2? Yes.
isequal(S, S2)
  4 Comments
Steven Lord
Steven Lord on 10 Mar 2022
If you already have the vectors of indices and the vector of values, you don't need to call find. I didn't have them (I had the original sparse matrix) so I used find to generate those vectors. Start with the next step.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!