This is my second time posting please only answer IF YOU ARE GOOD AT MATLAB PLEA
ID: 3798002 • Letter: T
Question
This is my second time posting please only answer IF YOU ARE GOOD AT MATLAB PLEASE. The code I have is almost complete but I need it to run for if n= 10 correctly. I NEED AN IF ELSE STATMENT. included but i need help getting it. please do not post answer until your output matches this;;
n = 10; correct = [0 1 0 1 0 1 0 1 0 1]; and n = 11; correct = [1 0 1 0 1 0 1 0 1 0 1]; Write a function that takes in a number, n, as an input and has one output, an array of n that alternates 1's and 0's. IF the length of the array is odd, start the array with a 1. lf the length of the array is even, start the array with a 0. Please use a loop to accomplish this For example: [array] alternating (10) Would return the array: array [0 1 0 1 0 1 0 101] While array alternating (11) Would return the array array [1 0 1 0 1 0 1 0101] You can use the mod function to determine whether a number is even or odd. mod(n,2) will return 1 if n is odd and 0 if n is even My Solutions Try Again Activity Summary You have submitted: 6 solutions Maximum Submissions: Unlimited solutions Most recent best submission: 6th Submission: 1 out of 3 tests passed Test Results Submitted on 21 Feb 2017 at 4:22 Solution ID: 3648352 function array alternating (n) array rand (1, n) for i 1 n array (i) mod (i,2); end end
Explanation / Answer
function lst = alternating(n)
lst = zeros(1,n);
if mod(n,2) == 1
for i = 1:2:n
lst(1,i)=1;
end
else
for i = 2:2:n
lst(1,i)=1;
end
end
end
alternating(10)
alternating(11)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.