如何在

时间:2017-02-18 17:02:09

标签: matlab

如何为下面的条件保存x的值,并为它们分配y以查找其长度

    for i=1:100
    n=1;
    x(i)=rand
    if x>0.5
    y=x;
    end
    length(y)
    end

1 个答案:

答案 0 :(得分:1)

这里不需要循环。执行以下操作:

x = rand(1,100); %Storing all values at once
y = x(x>0.5);    %Storing values of x greater than 0.5 in y
length(y)        %Finding the length of y

建议阅读:
Matrix Indexing in MATLAB
by Steve Eddins and Loren Shure (MathWorks)