我正在尝试获取数组每行的标签以实现RLE编码,其中标签的格式为[数组中的#,行中重复的数字]。
我得到的数组最小为2x2,最大为10 x10。目的是要知道每行重复多少个数字,并得到上面解释的结果
我试图与for周期进行比较,但没有得到期望的结果并带有标志,但是我不能
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_)
答案 0 :(得分:0)
您所需的输出从您的帖子中不是很清楚。但是,我相信您有一个矩阵,并且您正在尝试计算逐行重复多少个矩阵元素。
我为您准备了以下代码,以了解如何逐元素遍历矩阵以比较行中的每个元素。
该代码非常不言自明。如果您仍然遇到麻烦,请不要犹豫。
(注意:有许多比下面的代码更有效的方法来执行此任务)。 (注2:我的代码告诉您在输出语句中的一行中出现了多少个元素,您将在下面看到。如果只希望“重复”元素的数量,则从相应的结果中减去1。)
希望这会有所帮助。
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)