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

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)

Related

How do you count the sum of arithmetic progression using while loop? [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 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.

How to make a new matrix based on column values in an old matrix? [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 2 years ago.
Improve this question
I am very new to Matlab and coding in general, so I apologize if this is a basic question.
I have a matrix of three columns (data1) where the first column refers to time (s).
I would like to make a new matrix (bout1) consisting of entire rows of the matrix data1 based on the values in the first column (so, for example, in a range from 30 s to 120 s).
I know how to extract the rows based on the row number:
bout1 = data1(361126:391643,:)
but not based on the values in a specific column.
You can use the find function (see here) to find the rows you need, like this:
time = data1(:, 1);
i = find(30 <= time & time <= 120);
bout1 = data1(i, :);

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.

How can I trace the highest value in a matrix until I go to a cell in the matrix with zero 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 6 years ago.
Improve this question
I need to start with the highest value in the matrix (i,j). Then, go backwards to one of positions (i-1,j), (i, j-1), and (i-1, j-1) depending on the direction of movement used to construct the matrix. This method is used throughout until a matrix cell with zero value is reached.The above image shows what I want, I need to trace those marked in blue. I know there is a max function in matlab.
If I understand what you try to do...
Suppose M is your matrix:
result=[];
[~,ind]=max(M(:));
[r,c]=ind2sub(size(M),ind);
result=[result; r c];
while M(r,c)~=0
[~,f]=max([M(r-1,c),M(r,c-1),M(r-1,c-1)]);
switch f
case 1
r=r-1;
case 2
c=c-1;
case 3
r=r-1;
c=c-1;
end
result=[result; r c];
end
Then, result is a 2 x n matrix, where each row is the indexes of one step- from the upper to lower.

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)