显示特定数字是否在数组-matlab中

时间:2013-04-11 19:38:45

标签: matlab

该计划

clc;
clear;
d =input('enter the hop count of all path');  % [ 1 2 3]
en=[1 0 0 0 0 0 0;   1 1 1 1 1 1 1 ;   1 1 0 0 0 0 0; 1 0 1 0 1 0 1; 1 1 1 1 0 0 0]
c = cumsum(d);    % [1, 3, 6]
s = ceil(size(en,1)*c/c(end));    % [2, 5, 9] 
n = [s(1) diff(s)]; % [2, 3, 4]
B = mat2cell(en, n, size(en,2)); 
n=1;
for i=1:length(d)

disp(sprintf('the transmiting frame thr path %d ',n));
disp(B{i});
n=n+1;
end
disp ('SINGLE BIT ERROR');
        S=randint(1,1,[1,size(en,1)]);
        T=randint(1,1,[1,size(en,2)]);
        if (en(S,T)==1)
            en(S,T)=0;
        elseif (en(S,T)==0)
            en(S,T)=1;
        end
        disp(en);
        disp('SINGLE BIT ERROR INTRODUCED AT');
        disp(S);               %row
        disp(T);                % column
        p=S*T; 
        disp('error at bit');
        disp(p);                       %displays which bit is error

for i=1:length(d)
    disp(sprintf('size of transmitted frame %d',i));
    disp(size(B{i}));              % size of each transmitted frame
    ee(i)=(size(B{i},1)*size(B{i},2)); 
end
disp('display (row*column) of each frame');
    disp(ee(:));               % displays (row*column) of each frame
kk=cumsum(ee(:));
disp('the cumulative sumation is ');
disp(kk);                    % cumulative sumation of the elements in ee

输出将是

enter the hop count of all path[ 1 2 3]

en =

     1     0     0     0     0     0     0
     1     1     1     1     1     1     1
     1     1     0     0     0     0     0
     1     0     1     0     1     0     1
     1     1     1     1     0     0     0

the transmiting frame thr path 1 
     1     0     0     0     0     0     0

the transmiting frame thr path 2 
     1     1     1     1     1     1     1
     1     1     0     0     0     0     0

the transmiting frame thr path 3 
     1     0     1     0     1     0     1
     1     1     1     1     0     0     0

SINGLE BIT ERROR
     1     0     0     0     0     0     0
     1     1     1     1     1     1     1
     1     1     0     0     0     0     0
     1     0     1     0     1     1     1
     1     1     1     1     0     0     0

SINGLE BIT ERROR INTRODUCED AT
     4

     6

error at bit
    24

size of transmitted frame 1
     1     7

size of transmitted frame 2
     2     7

size of transmitted frame 3
     2     7

 display (row*column) of each frame
     7
    14
    14

the cumulative sumation is 
     7
    21
    35

>> 

因此p = 24存在于kk(2)和kk(3)之间。它应该显示'第3帧有错误'。同样地,如果p <= kk(1),它应该显示'第1帧有错误'并且如果p在kk(1)和kk(2)之间,它应该显示'第2帧有错误'。但是长度(d)随着用户的输入而相应地变化。我无法像这样显示它。请帮帮我。

1 个答案:

答案 0 :(得分:0)

使用SPRINTF功能。由于kkcumsum函数的输出,因此它已排序,您可以使用find来查找帧索引。

if p <= kk(end)
    idx = find((kk-p)>=0, 1);
    disp( sprintf('frame %d has error',idx) )
end