简体   繁体   中英

Storing for loop output as vector?

I'm trying to store the output of my n and b variables as vectors. The solutions I have tried produce an unnecessary amount of zeros in the vector. Any suggestions?

x=rand(113,1);
y=rand(113,1);
x=sort(x, 'descend');
v=[x,y];
for i=2:113
  m=max(v(1:i-1,2));
  if v(i,2) > m == true 
    n=(v(i-1,2))
    b=(v(i-1,1))
  end 
end
x=rand(113,1);
y=rand(113,1);
x=sort(x, 'descend');
v=[x,y];
n=[];b=[];
for i=2:113
   m=max(v(1:i-1,2));
   if v(i,2) > m
        n(end+1)=v(i-1,2)
        b(end+1)=v(i-1,1)
   end 
end
x=rand(113,1);
y=rand(113,1);
x=sort(x, 'descend');
v=[x,y];
n_vect = [];
b_vect = [];
for i=2:113
   m=max(v(1:i-1,2));
        if v(i,2) > m == true 
            n=(v(i-1,2))
            b=(v(i-1,1))

            n_vect = [n_vect,n];
            b_vect = [b_vect,b];

        end 
end

First of all, you do not need the true on the if line. When you say if v>m it means if v is bigger than m so not need to have the == true .

You can refine your code as follows:

x=rand(113,1);
y=rand(113,1);
x=sort(x, 'descend');
v=[x,y];
N = [];
B = [];
for ii=2:113
    if v(ii,2) > max(v(1:ii-1,2))
        N = [N v(ii-1,2)];
        B = [B v(ii-1,1)];
    end
end

But remember if the vectors are large you should consider preallocating the two vectors. If you do not know the size of an array before the loop begins, preallocate it, and then if necessary, shrink it after the loop completes.

In that case you can use NaN to preallocate and then get rid of them:

x=rand(113,1);
y=rand(113,1);
x=sort(x, 'descend');
v=[x,y];
N = nan(size(x));
B = N;
for ii=2:113
    if v(ii,2) > max(v(1:ii-1,2))
        N(ii,1) = v(ii-1,2);
        B(ii,1) = v(ii-1,1);
    end
end
N = N(~isnan(N));
B = B(~isnan(B));

note that using:

a = [] ;
for i = 1:n
   a = [a i] ;
end

The above method will take high time when your looping to a higher number. Also, note that MATLAB shows redline under the array a . So, best way is to preallocate the matrix. Now, question is, how I can preallocate a matrix/array when I don't know the dimensions. Remember we are in MATLAB and it has ton's of options. We are lucky that, we know one dimension in this case, ie your result is an array, so one of the dimension is 1. We can preallocate the result in this case. Check the below code:

x=rand(113,1);
y=rand(113,1);
x=sort(x, 'descend');
v=[x,y];
count = 0 ;
n_vect = zeros(1,[]);
b_vect = zeros(1,[]);
for i=2:113
    m=max(v(1:i-1,2));
    if v(i,2) > m == true
        n=(v(i-1,2)) ;
        b=(v(i-1,1)) ;
        count = count+1 ;
        n_vect(count) = n ;
        b_vect(count) = b;

    end
end

Now MATLAB doesn't show any warning, as we have preallocated it.

Note that, it is better to always terminate the lines with ; to avoid unnecessary prints on the screen, which are annoying and time consuming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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