To concatenate rows in matrix within for loop based on if else statement

조회 수: 2 (최근 30일)
om prakash
om prakash 2019년 9월 24일
답변: om prakash 2019년 10월 3일
Hello everyone,
m = 2;
PU = 4;
NumBits=1000;
lamda_val=0.5;
si=rand(m,NumBits);
hi=rand(m,NumBits);
ni=rand(m,NumBits);
PU = input('Enter No. of primary user = ');
Fc = zeros(1, PU);
if PU >=1
for x = 1:PU
Fc(x) = input('Enter 1 for signal present and 0 for noise in channel = ');
end
else
disp('No primary signal detected');
return;
end
table = zeros(m,PU*NumBits);
for index = 1:(PU)
if Fc(index) == 1
for ii=1:m
temp(ii,:)=abs((hi(ii,:).*si(ii,:))+ni(ii,:)).^2;
end
else
for ii=1:m
temp_noise(ii,:)=abs(ni(ii,:)).^2;
end
end
table = [temp(ii,:) temp_noise(ii,:)]
end
can somebody tell me how to do this. I have to store values in rows.
I have edited the code for better understanding. Hope it helps.
If PU is 4 with values 1011, then values generated due to these values should be stored in concatenation to evaluate further.
Regards
Om Prakash
  댓글 수: 3
Fabio Freschi
Fabio Freschi 2019년 9월 24일
your example is not working because Fc is not defined
om prakash
om prakash 2019년 9월 26일
hi kalyan
I have edited my question, any suggestion?

댓글을 달려면 로그인하십시오.

답변 (2개)

Jos (10584)
Jos (10584) 2019년 9월 24일
This code is weird.
In each iteration of "index" you fill either a matrix temp or a matrix temp_noise with m rows of values.
Then you concatenate only the last (= ii-th) row of both of these matrices into a single row, and store this in the variable table. One the of these last rows was just created, and for the other one you use the one created one or more iterations ago.
At every subsequention iteration, this variable table is overwritten.
So, first you need to carefully think about what you are trying to do ....
  댓글 수: 2
Fabio Freschi
Fabio Freschi 2019년 9월 24일
I agree, I was trying to reverse engineer the problem from the code, but a clear example would help
om prakash
om prakash 2019년 9월 26일
Hi jos,
I have edited my question, any suggestion?

댓글을 달려면 로그인하십시오.


om prakash
om prakash 2019년 10월 3일
m = 2;
PU = 4;
NumBits=1000;
lamda_val=0.5;
si=rand(m,NumBits);
hi=rand(m,NumBits);
ni=rand(m,NumBits);
PU = input('Enter No. of primary user = ');
Fc = zeros(1, PU);
if PU >=1
for x = 1:PU
Fc(x) = input('Enter 1 for signal present and 0 for noise in channel = ');
end
else
disp('No primary signal detected');
return;
end
table = zeros(m,PU*NumBits);
for index = 1:(PU)
if Fc(index) == 1
for ii=1:m
temp(ii,:)=abs((hi(ii,:).*si(ii,:))+ni(ii,:)).^2;
A(ii,:) = temp(ii,:);
end
else
for ii=1:m
temp_noise(ii,:)=abs(ni(ii,:)).^2;
A(ii,:) = temp_noise(ii,:);
end
end
table(index*ii,:) = A(ii,:);
%then it need to be transposed
end
I tried this, but table matrix is not storing the correct values. Apart from that it need to be transposed to convert matrix [2,4000]
can anyone tell me how this can be done.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by