Building a 3 column vector results in a 157 column vector instead?

40 views Asked by At

In my for loop I am trying to build a vector from local variables. But something weird is happening. When I build my vector I expect it to contain 3 doubles (1x3). But instead it contains hundreds doubles (1x1XX).

What the heck is going wrong? Am I misunderstanding m's scope or vector building?

for i=0:ncols
  for j=0:nrows

    ...

    roi = imcrop(img, [cx, cy, cw, ch]);

    %Extract 3 channels
    red = roi(:,:,1);
    green = roi(:,:,2);
    blue = roi(:,:,3);

    %Get most frequent colour
    [freqR, f, c] = mode(red);
    [freqG, f, c] = mode(green);
    [freqB, f, c] = mode(blue);
    freqR = double(freqR) / 255;
    freqG = double(freqG) / 255;
    freqB = double(freqB) / 255;

    %ERROR OCCURS ON BELOW LINE
    %Build vector representing most common colour in cell
    col = [freqR,  freqG,  freqB];

    %Intended usage of col 
    rectangle('Position', [cx, cy, cw, ch], 'EdgeColor', col, 'LineWidth', 3, 'LineStyle','-');
  endfor
endfor
1

There are 1 answers

0
rahnema1 On BEST ANSWER

mode ,and some other functions in MATLAB, if applied to an array, return the result along the dimension provided as the second input to the function if is not provided it defaults to the first dimension.When you write mode(red) it is the same as mode(red, 1).

So you need to convert the matrix to a column vector:

[freqR, f, c] = mode(red(:));