1
votes

This is quite a simple issue, but I've been struggling with it. sortedd and sortedfinal_d are 8 x 1000 Matrices and I am using the loop below to check if any of the elements in sortedfinal_d lies between two consecutive elements of sortedd, in terms of magnitude. I'm doing this along each row. overall_p is a 8 x 1000 Matrix as well, but at the end of this process I end up having final_p as a Matrix of Zeros. I don't know why this is.

for k=2:1000
    for s=1:1000
        for j=1:8
            if sortedd(j,k) > sortedfinal_d(j,s) && sortedfinal_d(j,s) > sortedd(j,k-1)
                final_p(j,s) = overall_p(j,k);
            end
        end
    end
end

EDIT: Added data for the inputs as shown below: sortedd (first four columns) =

   0.219977361620113   0.219996752039812   0.220344444223787   0.220593274018691
   0.272807483153955   0.273682693068593   0.273846498221277   0.274060049642900
   0.327201460264565   0.327375792227635   0.327572790857546   0.327856448530021
   0.380389118311424   0.380845274148177   0.380893687870765   0.381015090963159
   0.434832574575088   0.434860658844550   0.435021604722982   0.435119929919457
   0.487119089589798   0.488128501559782   0.488207451439073   0.488430455768512
   0.540652551559395   0.541303305046034   0.542195194863130   0.542234381085921
   0.595254195563241   0.595296064375604   0.595376090156252   0.595377962767971   

sortedfinal_d =

   0.182086792394190   0.182406508309366   0.182406508309366   0.182808976400818
   0.233058295607543   0.233058295607543   0.233158455616954   0.233158455616954
   0.286243848617693   0.286357973626582   0.286918095670684   0.287393171241241
   0.336938335090164   0.336938335090164   0.337094505106945   0.337669618738100
   0.390287818652551   0.390567879874952   0.390567879874952   0.390670502700602
   0.446995120903824   0.447270251510681   0.447452123072880   0.447597175111267
   0.501060785098551   0.501060785098551   0.501060785098551   0.501060785098551
   0.551311219045087   0.551463923687602   0.551463923687602   0.551653815175502

Thanks a lot

2
Is ‘final_p' 8x1000 too? You have mentioned - “if any of the elements in sortedfinal_d lies between 2 cons. elements of sortedd, in terms of magnitude…. I'm doing this along each row”. In each row this condition could be met several times or never. My question - If this condition is met, how would you change ‘final’? Should ‘final’ become equal to the corresponding element in overall_p, that is should we assume that sortedfinal_d(3,4) = overall_p(3,4) or do we have to put sortedfinal_d(3,4) = overall_p(3,y), where y is the first occurrence of the condition being met for that row? - Divakar

2 Answers

3
votes

Do you have to use loops to accomplish this?

matching_d = sortedfinal_d(:,1:end-1) < sortedd(:,2:end) ...
                  & sortedd(:,2:end) < sortedfinal_d(:,2:end);
final_p(matching_d) = overall_p(matching_d);

If you can show us a small sample input (say, 1x5 versions of sortedd and sortedfinal_d) and output (the corresponding matching_d) it would be easier for us to help troubleshoot.

1
votes

If i set your matrices to random numbers, final_p does return some numbers.
So your code works as is. Post your dataset or at least describe the dataset in some detail, this will make it much easier to diagnose the problem.

I have reduced 1000 down to 10 and re-ordered the iteration variables to i, j, k to make it easier to follow:

sortedd = rand(8, 10);
sortedfinal_d = rand(8, 10);
overall_p = rand(8, 10);

for i=2:10
    for j=1:10
        for k=1:8

            if sortedd(k,i) > sortedfinal_d(k,j) && sortedfinal_d(k,j) > sortedd(k,i-1)
                final_p(k,j) = overall_p(k,i);
            end
        end
    end
end

final_p