2

I have a 5x5 matrix, V1, with values and a classification in both the first row and first column. If you wanted to, you could think of sectors in an economy that have a classification. The first two sectors have a 3-digit classification, the last two have a 4-digit classification.

V1 =

             0         101         111        1234        1111
           101           4           0           7           0
           111           5           8           0           0
          1234           6           0           6           2
          1111           0           0           4           9

Now, I want to rediagonalize all columns that have a 4-digit code using MATLAB. That means in four-digit columns the values should be summed up over the whole column and shifted to the diagonal. In particular, the code should perform the following steps:

  1. If the classification code has four digits AND the classification code is equal in both the first row AND the first column, then sum up the whole column (excl. the first value of the column, which is the classification code itself)
  2. Elseif the classification code has three digits in the column, then leave the value as is
  3. Else assign a zero.

The resulting matrix should look like this:

V1 =

             0         101         111        1234        1111
           101           4           0           0           0
           111           5           8           0           0
          1234           6           0           17          0
          1111           0           0           0           11

I have tried the following code, but it didn't work:

[vrow vcol] = size(V1)
for c = 2:vcol;
   for r = 2:vrow;
        if all([ V1(1,c) == V1(r,1), numel(num2str(V1(1,c))) > 3, numel(num2str(V1(r,1))) > 3 ]) ;
           V1(r,c) = sum(V1(2:end,c)) ;
        elseif numel(num2str(V1(1,c))) == 3;
            V1(r,c) = V1(r,c);
        else
            V1(r,c) = 0;
        end
   end
end

With the above code, I got the following result, which is sort of close to the desired result, only the column summations do not work yet:

V1 =

               0         101         111        1234        1111
             101           4           0           0           0
             111           5           8           0           0
            1234           6           0          10           0
            1111           0           0           0           9

Thank you for any hints!

P.W.
  • 21
  • 1

1 Answers1

0

Since you're changing elements of V1 as the code executes, but also relying on values in V1 to get your answer (when you sum columns), you'll have errors if you set a value to 0 before it's needed in a sum. Below, I've fixed that issue by initializing a second matrix V2 as your output. I've also gone ahead and removed a few unneeded lines, as explained in the comments

[vrow vcol] = size(V1);
V2 = zeros(vrow, vcol);
for c = 2:vcol;
   for r = 2:vrow;
        if all([ V1(1,c) == V1(r,1), numel(num2str(V1(1,c))) > 3]) ; %Third statement was redundant
            V2(r,c) = sum(V1(2:end,c)) ;
        elseif numel(num2str(V1(1,c))) == 3;
            V2(r,c) = V1(r,c);
        end %We intialized whole thing to 0, so we don't need to set elements to 0
    end
end
Ian Riley
  • 523
  • 2
  • 8