1. 程式人生 > >matlab求矩陣、向量的模

matlab求矩陣、向量的模

求矩陣的模:

function count = juZhenDeMo(a,b)
[r,c] = size(a);%求a的行列
[r1,c1] = size(b);%求b的行列
count = 0;
for j=1:r-r1+1%所求的行數中取
    for i=1:c-c1+1%所有的列數中取
        d = a(j:j+r1-1,i:i+c1-1);
        e = double(d==b);
        if(sum(e(:))==r1*c1)
            count = count + 1;
        end
    end
end<pre name="code" class="plain">clc;
clear;
a = eye(6)
b = [1 0;0 1]
disp('a矩陣中b的模的個數是:');
count = juZhenDeMo(a,b)

end
求向量的模:
function count = sta_submatrix1(a,b)
count = 0;
for i = 1:length(a)-length(b)+1
    c = a(i:i+length(b)-1);
    e = double(c==b);
    if(sum(e) == length(b))
        count = count + 1;
    end
end
end
clc;
clear;
a = [0 0 0 1 0 0 1  0 0 1 0 0 1 0 0]
b = [0 0 ]
disp('b在a中的模的個數是:')
count = sta_submatrix1(a,b)