0

I have a 3020X1860 matrix srtloc that looks like this:

  1     2   3  . . . 1860 
______________
1|31    77  88
2|88    213 235
3|213   235 304
4|235   304 327
.
.
.
3020

I want to create a new matrix that has

first row: 1 in the 31st column, a 2 in the 77th column, 3 in 88th column...

second row: 1 in the 88th column, 2 in 213th column, 3 in 235th column...

third row: 1 in the 235th colum, 2 in 304th column, 3 in 327th column...

and so on. To do this right now, I'm using nested for loops, and having speed issues:

pctrank  = NaN(3020,1860)
for rkix1 = 1:3020
    for rkix2 = 1:1860
        rowz = srtloc(rkix1,:);
        pctrank(rkix1,rowz(rkix2)) = rkix2;
    end
end

Is there a vectorized way to do this?

siegel
  • 819
  • 2
  • 12
  • 24
  • 1
    perfect case for `sub2ind` and has been discussed many times before, one of those - http://stackoverflow.com/questions/19122715/convert-a-for-loop-into-a-vector-vectorization – Divakar Aug 03 '14 at 05:35
  • 1
    Also, check this answer for some general approach... http://stackoverflow.com/questions/17540681/improving-matlab-matrix-construction-code-or-code-vectorization-for-begginers/17585725#17585725 – bla Aug 03 '14 at 05:36
  • Hey Divakar, sorry for the duplicate question, but can you help me see what the vectors `A` `B` and `C` are in my situation? I can't see the parallel between my problem and the link you posted. – siegel Aug 04 '14 at 13:28
  • [~,pctrank] = sort(srtloc,2); actually solves my problem, without sub2ind. – siegel Aug 04 '14 at 14:02

0 Answers0