user3482135
user3482135

Reputation: 75

In matlab, how to concatenate value calculated in a for loop?

In matlab, if I have a code that calculates the rate of something in a for loop like this

for t=1:10
Ratek=(1/2)*(log2(1+SINR1k)+log2(1+SINR2k));
end

and each time the value of the rate differs, I want in to concatenate all the values of this rate in each loop so that after the 10th loop I have the 10 rates calculated to use them. How can I do this ?

Upvotes: 0

Views: 178

Answers (3)

Stewie Griffin
Stewie Griffin

Reputation: 14939

If your data is static and you only want do duplicate the same result 10 times, then

Ratek(1:10) = (1/2)*(log2(1 + SINR1k)+log2(1 + SINR2k));

But, that would not be of much use, so I'm going to assume that SINR1k and SINR2k are 10x1 vectors.

SINR1k = randi(10,10,1);
SINR2k = randi(10,10,1);

Now, you could do:

Ratek = zeros(10,1); %// Important to preallocate memory to avoid growing vectors inside loops

for t=1:10
   Ratek(t) = (1/2)*(log2(1 + SINR1k(t))+log2(1 + SINR2k(t)));
end

This is a much better approach than starting with an empty matrix and adding new elements to it. However, in MATLAB loops can be avoided in cases like this, so the following line will give the same result as the loop above:

Ratek = (1/2) * (log2(1 + SINR1k)+log2(1 + SINR2k));

Benchmarking:

n = 100;   %// A bit bigger datasample
SINR1k = randi(100,n,1);  %// Random numbers
SINR2k = randi(100,n,1);  %// Random numbers

%//Approach 1, growing vectors:
tic
for ii = 1:1000   %// Several runs to account for warm-up etc.
  Ratek = [];
  for t=1:n
    Ratek(end+1) = (1/2)*(log2(1+SINR1k(t))+log2(1+SINR2k(t)));
  end
end
toc
Elapsed time is 0.291688 seconds.

%// Approach 2, pre-allocation:
tic
for ii = 1:1000   %// Several runs to account for warm-up etc.
  Ratek = zeros(n,1);
  for t=1:n
    Ratek(t) = (1/2)*(log2(1+SINR1k(t))+log2(1+SINR2k(t)));
  end
end
toc
Elapsed time is 0.070791 seconds.

%// Approach 3, vectorization:
tic
for ii = 1:1000
    Ratek = (1/2)*(log2(1+SINR1k)+log2(1+SINR2k));
end
toc
Elapsed time is 0.056612 seconds.

As the benchmark results above show, preallocation makes it more than 4 times as fast whereas vectorization improves performance even more.

If you actually only want to duplicate the results:

tic
for ii = 1:1000
    Ratek(1:n) = (1/2)*(log2(4)+log2(5));
end
toc
Elapsed time is 0.010811 seconds.

Upvotes: 1

Igwe Kalu
Igwe Kalu

Reputation: 14868

How about this, building on @Joachim's answer:

Ratek = []
for t=1:10
    val = (1/2)*(log2(1+SINR1k)+log2(1+SINR2k));
    if ~Any(Ratek == val)
        Ratek(end+1) = val;
end

If you want to contenate as string:

num2str(Ratek)

I'm not really into MATLAB, but that's my take.

Upvotes: 1

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

I assume you want to concatenate the calculation result to a vector; you can then assign to Ratek(end+1) to append the value;

Ratek = []
for t=1:10
  Ratek(end+1) = (1/2)*(log2(1+SINR1k)+log2(1+SINR2k));
end

(assuming the real calculation gives a separate value each time instead of the static example)

As a side note, growing vectors and loops may really hurt performance if you do it frequently, so be aware of the performance implications.

Upvotes: 1

Related Questions