-1

I'm trying to get labels per row of an array to implement the RLE encoding, where the tag has the format [# in the array, number repeated in row].

I get an array of minimum 2x2 and maximum of 10 x 10. The objective is to know how many numbers are repeated per row and get the result explained above

I tried to compare with a for cycle but I do not get the desired result and with flags but i can´t

matriz = randi([0,127],horizontal,vertical);
        set(handles.matriz,'String',num2str(matriz));
        for i= 1:horizontal
            cantidad = 0;
            for j = 1:vertical
                num = matriz (i,j);
                if(num == matriz(??))
                    contador = contador +1;
                end
                position = strcat('[',num2str(num),',''num2str(contador)',']');
            end
        end
        set(handles.bloques,'String',matriz_)

1 Answers1

0

The output you require is not very clear from your post. However, I believe you have a matrix and you are trying to count how many of the matrix elements are repeated in a row by row basis.

I have prepared the below code for you to understand how to iterate through the matrix element by element to compare each elements with other in its row.

The code is pretty self-explanatory. If you still have trouble, please do not hesitate to say so.

(NOTE : There are many more efficient ways to do this taks than the code below.) (NOTE2: My code tells how many elements occured in a row in the output sentence you will see below. If you only want the number of "repeating" elements just subtract 1 from the corresponding result.)

Hope this helps.

function resultList = findRepeatingElementsRow()

matrix = randi(10,5);

numberOfRows = size(matrix, 1);
numberOfColumns = size(matrix, 2);

for i = 1:numberOfRows
    for j = 1:numberOfColumns
        matrixElement = matrix(i,j);
        counter = 0;
        for k = 1:numberOfColumns
            compareElement = matrix(i,k);
            if matrixElement == compareElement
                counter = counter + 1;
            end
        end
        msg = strcat('In row number', char(20), num2str(i), char(20), 'Matrix Element', char(20), num2str(matrixElement), char(20), 'is repeated', char(20), num2str(counter), char(20), 'time(s)');
        disp(msg);
    end
end
    10     6     2     1     8
     5     3     3     3     5
     2     7     4     9     6
     3     8     5     1     3
     5     3     6    10     5

In row number 1 Matrix Element 10 is repeated 1 time(s)
In row number 1 Matrix Element 6 is repeated 1 time(s)
In row number 1 Matrix Element 2 is repeated 1 time(s)
In row number 1 Matrix Element 1 is repeated 1 time(s)
In row number 1 Matrix Element 8 is repeated 1 time(s)
In row number 2 Matrix Element 5 is repeated 2 time(s)
In row number 2 Matrix Element 3 is repeated 3 time(s)
In row number 2 Matrix Element 3 is repeated 3 time(s)
In row number 2 Matrix Element 3 is repeated 3 time(s)
In row number 2 Matrix Element 5 is repeated 2 time(s)
In row number 3 Matrix Element 2 is repeated 1 time(s)
In row number 3 Matrix Element 7 is repeated 1 time(s)
In row number 3 Matrix Element 4 is repeated 1 time(s)
In row number 3 Matrix Element 9 is repeated 1 time(s)
In row number 3 Matrix Element 6 is repeated 1 time(s)
In row number 4 Matrix Element 3 is repeated 2 time(s)
In row number 4 Matrix Element 8 is repeated 1 time(s)
In row number 4 Matrix Element 5 is repeated 1 time(s)
In row number 4 Matrix Element 1 is repeated 1 time(s)
In row number 4 Matrix Element 3 is repeated 2 time(s)
In row number 5 Matrix Element 5 is repeated 2 time(s)
In row number 5 Matrix Element 3 is repeated 1 time(s)
In row number 5 Matrix Element 6 is repeated 1 time(s)
In row number 5 Matrix Element 10 is repeated 1 time(s)
In row number 5 Matrix Element 5 is repeated 2 time(s)
byetisener
  • 310
  • 2
  • 11