How do you count the sum of arithmetic progression using while loop? [closed] - matlab

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm new to MATLAB and coding, so any help is much appreciated. I wanted to make a program that calculates the sum of an arithmetic progressions of natural numbers where you input N as the total natural number of terms in the sequence and the common difference between the terms is 1 and calculating it using the while loop. I have no idea how to code this program so I will be much grateful if anyone could help me. Thanks in advance!

total number stands for N in your question. diff stands for difference of each term in arithmetic progression. start stands for initial value of given arithmetic progression.
total_number=5;
diff=3;
start=1;
sum=0;
i=1;
while i<=total_number
tmp=start+diff*(i-1);
sum=sum+tmp;
i=i+1;
end
I hope this answer helps.

Related

Intertwine 0s betweeen bits SystemVerilog [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to intertwine 0s between the bits of a variable. For example, imagine I have a variable a as follows
a = 1101
Then, I want to obtain the following result
b = 1_000000_1_000000_0_000000_1
That results from intertwining 6 0s betwen each bit of a. I was wondering if there was any elegant way of doing this in SystemVerilog. Any help is appreciated.
b = 0;
foreach (a[i]) b[i*7] = a[i];

Is there a faster way to calculate the expected value? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So for my probability class, my professor asked the following question on a homework problem:
A fair coin is flipped 10,000 times. Let X correspond to the difference between the number of heads and the number of tails. Using MATLAB, compute the expected value of X.
This is what I wrote to answer the question:
N = 10000;
i =0;
r=1/2;
Q=nchoosek(N,(X+N)/2);
X=(1,N);
for i=-N:N
P=Q*r.^(X+N)/2*(1-r)^(N-(X+N)/2) % probability_mass_function
E=sum(abs(X).*P); % expected value
end
However, is there faster and quicker way to compute this expected value? Any help would be greatly appreciated. Thanks
You can put all tests results in single matrix in one line and then calculate X per test, then average over X:
clear
TAIL=0; HEAD=1;
NumTests=121;
NumRollsPerTest=10*1000;
AllTestsRolls= rand(NumTests,NumRollsPerTest)>0.5 ; %head when rand>0.5
XperTest=sum(AllTestsRolls==HEAD,2)-sum(AllTestsRolls==TAIL,2);%every row is test so calc per test
ExpectedX=sum(XperTest)/length(XperTest)

Matlab : Opposite function of "primes" in matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is there any opposite function of "primes" in matlab?
(Primes : Prime numbers less than or equal to input value)
What if i want to find greater than or equal to input value?
Thank you.
You always have to provide an upper limit. If you wanted prime numbers "greater than or equal to the given value," they would go to infinity.
I would suggest starting with "less than or equal to," provide the function with an upper limit, and then use a conditional to filter out those that are too small.

loop to add present state with previous state [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to make a loop in order to add the current state with the previous state.
For example:
M=1000;
for i=1:M A=i*(x(i));
This formula will be for M=1 but when M=2 the formula will be like this:
A=(i*(x(i))+((i-1)*(x(i-1)))
and when M=3 the formula will be
A=(i*(x(i))+((i-1)*(x(i-1)))+((i-2)*(x(i-2))
and so on till reach the maximum length of M which is 1000.
Your question is quite vague but it sounds like you just want a cumulative sum of the i*x(i) series:
i = 1:M;
s = i.*x(i);
cumsum(s);

matlab coding for mean value of one row [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I need matlab coding for measuring mean value of one row in a matrix of 180 by 50 rows and columns. Each time the number of row in a matrix needs to be updated to get mean value of next row (like new_row=1:1:180). Kindly respond as soon as possible
You are looking for mean().
mean(A,2)
This will give you the means of each row of matrix A.
P.S.: If you already save one row in a vector, say new_row, you can simply use:
mean(new_row)