Understanding how to apply for loop ?

Hello all, I am new to MATLAB and hence finding difficulty in understanding how should I apply for loop in the following case:
for t = 1,2,3.....
s^t = [snr1^(t-1), snr2^(t-1), I^(t-1)]
end
where snr1, snr2 and I are some variables.
I am getting confused due to presence of t-1.
Any help in this regard will be highly appreciated.

댓글 수: 4

assigning your function to "s^t" does not work as that's not a valid variable name. calling it just "s" works:
snr1=2;
snr2=3;
I=1;
for t=1:3
s = [snr1^(t-1), snr2^(t-2), I^(t-1)]
end
s = 1×3
1.0000 0.3333 1.0000
s = 1×3
2 1 1
s = 1×3
4 3 1
chaaru datta
chaaru datta 2022년 11월 4일
Thank you sir for your answer. But (t-1) is not in power but it is the time instant i.e., for loop starts at t = 1 and t-1 corresponds to its previous instant.
well that cannot work because how do you want to adress t-1 if there was no previous instance in the first loop?
if you start later it'd work somehow like this:
snr1=2;
snr2=3;
I=1;
n=5;
t=[1:n]
t = 1×5
1 2 3 4 5
for i=3:5
s = [snr1^(t(i-1)), snr2^(t(i-2)), I^(t(i-1))]
end
s = 1×3
4 3 1
s = 1×3
8 9 1
s = 1×3
16 27 1
chaaru datta
chaaru datta 2022년 11월 4일
편집: chaaru datta 2022년 11월 4일
This is the part which I am trying to code.
Also "s" at time instant "t" depends on SNR1 at time instant (t-1), SNR3 at time instant (t-1), and I at time instant (t-1).

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

답변 (1개)

VBBV
VBBV 2022년 11월 5일

0 개 추천

SNR1 = rand(1,100).'; % the vector with SNR1 values
SNR3 = rand(1,100).';% the vector with SNR3 values
I = randi([1 100],[1 100]).'; % the integer vector with I values within (1-100)
for t = 2:length(SNR1)
s(t,:) = [SNR1(t-1); SNR3(t-1); I(t-1)].';
end
T = table(s(:,1),s(:,2),s(:,3),'VariableNames',{'SNR1','SNR3','I'})
T = 100×3 table
SNR1 SNR3 I ________ _______ __ 0 0 0 0.5286 0.55894 61 0.32919 0.89355 43 0.86775 0.43205 21 0.55857 0.25263 25 0.75068 0.78974 84 0.87684 0.35155 93 0.96913 0.97962 73 0.046261 0.81734 99 0.88748 0.59252 96 0.97548 0.30953 13 0.063743 0.37548 45 0.49473 0.4175 84 0.15575 0.89957 75 0.84632 0.63998 31 0.68992 0.57802 14

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2022년 11월 4일

댓글:

2022년 11월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by