-1

I need to get a 16x16 matrix of varying intensities, i.e from 0 to 255 from column 1 to column 16. This code, however, gives a varying intensity of size 1x271. How do I get 255 intensities in 16 columns?

for row = 1:16
    for col = 1:16
        i = 0;
        while i < 256
            X(col) = i;
            i = i+1;
            col = col + 1;
        end
    end
end

size(X)
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Draco
  • 83
  • 8
  • It's considered rather rude and impolite to delete your question directly after receiving an answer. Please read [What should I do when someone answers?](https://stackoverflow.com/help/someone-answers). Instantly deleting the question isn't on that list. – Adriaan Aug 28 '20 at 09:28

1 Answers1

2

Without loops: make an array 0:255 containing all your "intensity levels", then simply reshape() to a 16 -by- 16 matrix.

out = reshape(0:255,[16 16])
out =
     0    16    32    48    64    80    96   112   128   144   160   176   192   208   224   240
     1    17    33    49    65    81    97   113   129   145   161   177   193   209   225   241
     2    18    34    50    66    82    98   114   130   146   162   178   194   210   226   242
     3    19    35    51    67    83    99   115   131   147   163   179   195   211   227   243
     4    20    36    52    68    84   100   116   132   148   164   180   196   212   228   244
     5    21    37    53    69    85   101   117   133   149   165   181   197   213   229   245
     6    22    38    54    70    86   102   118   134   150   166   182   198   214   230   246
     7    23    39    55    71    87   103   119   135   151   167   183   199   215   231   247
     8    24    40    56    72    88   104   120   136   152   168   184   200   216   232   248
     9    25    41    57    73    89   105   121   137   153   169   185   201   217   233   249
    10    26    42    58    74    90   106   122   138   154   170   186   202   218   234   250
    11    27    43    59    75    91   107   123   139   155   171   187   203   219   235   251
    12    28    44    60    76    92   108   124   140   156   172   188   204   220   236   252
    13    29    45    61    77    93   109   125   141   157   173   189   205   221   237   253
    14    30    46    62    78    94   110   126   142   158   174   190   206   222   238   254
    15    31    47    63    79    95   111   127   143   159   175   191   207   223   239   255

One of the major problems in your code is the line col = col + 1; you're changing a loop index within a loop. That's bound to lead to weird and hard to debug errors.
A loop could look as follows:

X = zeros(16,16);  % Initialise X to the correct size
idx = 0;  % Initialise index

for cols = 1:16  % Columns first right?
    for rows = 1:16
        X(rows,cols) = idx;
        % X(idx+1) = idx;  % Uses linear indexing
        idx = idx+1;
    end
end

As a side note: i and j denote the imaginary unit. Using it as a variable can lead to hard to debug errors, thus I'd suggest to avoid it. Use ii, idx or a descriptive variable name.

Adriaan
  • 17,741
  • 7
  • 42
  • 75