-1

I have a database on a .csv like this:

1,2455,2.5,1260759113
1,2968,1.0,1260759200
1,3671,3.0,1260759117
2,10,4.0,835355493
2,17,5.0,835355681
2,39,5.0,835355604

And I would like to convert it into a matrix in Matlab but with the following characteristics:

  • The first element indicates de column of the matrix (starting from 1)

  • The second element corresponds to the row of the matrix (starting from 1)

  • The third element is the actual value that has to go in that (column ,row) of the matrix

  • The fourth element is garbage...

I am just getting started with Matlab so it would be great if you could provide me with some code because I don't even know how to google it right.

Matnagra
  • 49
  • 1
  • 11
  • 1
    Possible duplicate of [how to creat an array according to row and column number](https://stackoverflow.com/questions/36840287/how-to-creat-an-array-according-to-row-and-column-number) – beaker Jun 17 '17 at 16:06

1 Answers1

0
% Sample data
col = [1 1 1 2 2 2]'; % e.g. col = inputData(:,1)
row = [1 3 2 1 2 3]';
data = [11 12 13 14 15 16]';

% Using a loop: note that row and col must uniquely identify data

% preallocate output matrix
outMatrix = nan(max(row),max(col));

for i = 1:size(data,1)
    outMatrix(row(i),col(i)) = data(i);
end
  • It doesnt work. When I run the `for loop`, it gives me this error: `Attempted to access outMatrix(NaN,NaN); index must be a positive integer or logical.` – Matnagra Jun 17 '17 at 15:53
  • Could you check if the vectors row or col have any NaNs? E.g. sum(isnan(row)) should be zero. – EmptySpectrum Jun 18 '17 at 07:12