ELEFAAANT
ELEFAAANT

Reputation: 45

Additive loop matlab

Say i have a vector A 1x10000 double and i want to do some operations on this vector, and store it in a new vector B 1x1000000 double(A*100).

E.g. And i want to do an operation on A(1), lets say multiply by 2, do this 100 times. And place the results on place 1:100 in vector B. Then take A(2), and make the same operation, but store theese results on place 101:200 in vector B.

How can i do this?

Ive tried with a double for loop, but i dont know how to change it so the 2nd round gets stored on place 101:200.

This is the code ive been trying to do:

    % Random bitstream
msg = rand(1,10000) > 0.5;

% generate phaseshift bitstream with phaseshift +-180deg(+-1)
L = length(msg);

newmsg = zeros(1,L);
for i=1:L,
    if msg(i) == 0
        newmsg(i) = -1;
    else
        newmsg(i) = 1;
    end
end

% t = 0:.1:(L/100)-1;
t = 0:0.1:10-0.1;
fc  = 10e6;
fs = 2*fc;
sint = sin(2*pi*fc/fs*t);

%plot sine
plot(t*1/fs,sint);


%% This is the problem
Tx1 = zeros(1,L*length(t));
m = 0;
for j=1:L*length(t),
    m = m+1;
    if (m < L)
        for k=1:L,
            Tx1(k) = sint.*newmsg(m);
        end
    end
end 

Upvotes: 0

Views: 239

Answers (3)

nkjt
nkjt

Reputation: 7817

First, calculate your new values of A:

A2 = A*2;  % for your case, sint.*newmsg should work

Then use repmat and reshape to make a vector B with the appropriate values repeated n times each:

B = repmat(A2,[n,1]);
l = length(A2)*n;
B = reshape(B,[l,1]);

e.g. if A = 1:3, n = 10, the output is a 30 x 1 vector where each element in B(1:10), B(11:20),and B(21:30) all contain the values 2, 4, and 6 respectively.

Upvotes: 0

Luis Mendo
Luis Mendo

Reputation: 112689

Use

B = reshape(repmat(A*2,n,1),1,[])

where n is the size increase factor (100 or 2 in your examples).

See the doc of repmat and reshape if you are not sure how this solution works.

Upvotes: 1

User1551892
User1551892

Reputation: 3364

I hope, this code will solve your problem but you have to modify it a lil bit:

a = 1:1:10 ;
b = zeros(1,length(a)*10) ;
for (i =1:length(a)),
    k = ones (1,10) * a(i) ;
    for (jj = 1 : 10),
        bb(1,((i-1)*10)+jj) = k (i) ;
    end
end

Upvotes: 0

Related Questions