Matlab程序中temp(ic)= [data id]= out(ir) = Data(ir,id);这三句看不懂 程序为求Data每行出现最多的数Data = [1 2 3 2 2;4 3 1 5 4;3 5 1 2 1;3 3 1 1 3];for ir = 1:size(Data,1)%行数for ic = 1:size(Data,2)%列数temp(ic) = size(find(Dat
来源:学生作业帮助网 编辑:作业帮 时间:2024/08/13 23:36:48
![Matlab程序中temp(ic)= [data id]= out(ir) = Data(ir,id);这三句看不懂 程序为求Data每行出现最多的数Data = [1 2 3 2 2;4 3 1 5 4;3 5 1 2 1;3 3 1 1 3];for ir = 1:size(Data,1)%行数for ic = 1:size(Data,2)%列数temp(ic) = size(find(Dat](/uploads/image/z/12807781-61-1.jpg?t=Matlab%E7%A8%8B%E5%BA%8F%E4%B8%ADtemp%28ic%29%3D+%5Bdata+id%5D%3D+out%28ir%29+%3D+Data%28ir%2Cid%29%3B%E8%BF%99%E4%B8%89%E5%8F%A5%E7%9C%8B%E4%B8%8D%E6%87%82+%E7%A8%8B%E5%BA%8F%E4%B8%BA%E6%B1%82Data%E6%AF%8F%E8%A1%8C%E5%87%BA%E7%8E%B0%E6%9C%80%E5%A4%9A%E7%9A%84%E6%95%B0Data+%3D+%5B1+2+3+2+2%3B4+3+1+5+4%3B3+5+1+2+1%3B3+3+1+1+3%5D%3Bfor+ir+%3D+1%3Asize%28Data%2C1%29%25%E8%A1%8C%E6%95%B0for+ic+%3D+1%3Asize%28Data%2C2%29%25%E5%88%97%E6%95%B0temp%28ic%29+%3D+size%28find%28Dat)
Matlab程序中temp(ic)= [data id]= out(ir) = Data(ir,id);这三句看不懂 程序为求Data每行出现最多的数Data = [1 2 3 2 2;4 3 1 5 4;3 5 1 2 1;3 3 1 1 3];for ir = 1:size(Data,1)%行数for ic = 1:size(Data,2)%列数temp(ic) = size(find(Dat
Matlab程序中temp(ic)= [data id]= out(ir) = Data(ir,id);这三句看不懂 程序为求Data每行出现最多的数
Data = [1 2 3 2 2;
4 3 1 5 4;
3 5 1 2 1;
3 3 1 1 3];
for ir = 1:size(Data,1)%行数
for ic = 1:size(Data,2)%列数
temp(ic) = size(find(Data(ir,:) == Data(ir,ic)),2);
end
[data id] = max(temp,[],2);
out(ir) = Data(ir,id);
end
out'
Matlab程序中temp(ic)= [data id]= out(ir) = Data(ir,id);这三句看不懂 程序为求Data每行出现最多的数Data = [1 2 3 2 2;4 3 1 5 4;3 5 1 2 1;3 3 1 1 3];for ir = 1:size(Data,1)%行数for ic = 1:size(Data,2)%列数temp(ic) = size(find(Dat
1、第一句:
temp(ic) = size(find(Data(ir,:) == Data(ir,ic)),2);
以ir=1,ic=2为例说明
>> Data(1,:)%第一行所有的数
ans =
1 2 3 2 2
>> Data(1,2)%第一行第二列的数
ans =
2
>> find(Data(1,:) == Data(1,2))%找出 “Data(1,:)中大小为Data(1,2)的数” 所在的列的序号
ans =
2 4 5
>> size(find(Data(1,:) == Data(1,2)),2)%计算 “Data(1,:)中大小为Data(1,2)的数” 的数目
ans =
3
2、第二句:
C = max(A,[],dim) returns
the largest elements along the dimension of A specified
by scalar dim.For example,max(A,[],1) produces
the maximum values along the first dimension (the rows) of A.
[C,I] = max(...) finds
the indices of the maximum values of A,and returns
them in output vector I.If there are several identical
maximum values,the index of the first one found is returned.
[data id] = max(temp,[],2);%求出temp每行最大的数data ,并返回该数的序号id
3、第三句
out(ir) = Data(ir,id); %ir为行数,id为出现最多次数的数所在的列
temp(ic) = size(find(Data(ir,:) == Data(ir,ic)),2);
find(Data(ir,:) == Data(ir,ic)) 返回第ir行 中和 第ir行ic列相同的元素的下标
有一个相同,find就返回一个下标,有n个相同,就返回n个下标
再用size(find(...),2)统计find到的个数
因此在内曾循环结束后
全部展开
temp(ic) = size(find(Data(ir,:) == Data(ir,ic)),2);
find(Data(ir,:) == Data(ir,ic)) 返回第ir行 中和 第ir行ic列相同的元素的下标
有一个相同,find就返回一个下标,有n个相同,就返回n个下标
再用size(find(...),2)统计find到的个数
因此在内曾循环结束后
temp是个长度和原来矩阵列数一样的向量
第一个数纪律ir行中,与ir行1列相同的元素的个数
第二个数纪律ir行中,与ir行2列相同的元素的个数,依次类推
内层循环结束后,再根据temp找出出现最多的数
[data id] = max(temp,[],2);
max寻找temp中最大的数,数值返回给data,下标位置返回给id
由于max无论有多少个相同的最大值,只返回一个最大值
所以返回data和id都只是一个数,而data还没有用,我们只需要id
out(ir) = Data(ir,id);
输出的第ir个数 赋值为Data的第ir行,id列的数
由于之前的工作已经确定,第ir行,重复最多的数的列下标是id
所有Data(ir,id)就是ir行,出现最多的数把它赋值给out(ir)
外循环也结束的时候,所有行都统计完成
out(1) 就是第1行出现最多的数
out(2) 就是第2行出现最多的数
依次类推
收起