简体   繁体   English

Matlab-如何使用循环从一个向量创建两个向量

[英]Matlab- how to creat two vectors from one vector using loop

in this code I create a random vector with 100 values and I want to get two vector out of it: one with all the values under 1 and one with all the value above 1. I created this code, which works, but I wonder if there is a more efficient one, without to have the 2 vectors filled with 0 在这段代码中,我创建了一个具有100个值的随机向量,我想从中得到两个向量:一个向量的所有值都小于1,一个向量的所有值都大于1。我创建了此代码,它可以工作,但是我想知道是否有一种更有效的方法,而不必用0填充2个向量

clear all, close all, clc;
%% ficiional vector creation
%
a=0;
b=10;
v1=(b-a).*rand(100,1);
i=1;
while i<length(v1)  

if v1(i)>1
    x(i)=v1(i);
end
if v1(i)<1
        y(i)=v1(i);
end

        i=i+1;
end
x(x==0)=[];
y(y==0)=[];

thank you! 谢谢!

a=0;
b=10;
v1=(b-a).*rand(100,1);
x=v1(v1>1);
y=v1(v1<1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM