1. Make a vector from 1 to 100. Make a for-loop which runs through the whole vector. Multiply the elements which are smaller than 5 and larger than 90 with 10 and the other elements with 0.1.
1. Make a vector from 1 to 100. Make a for-loop which runs through the whole vector. Multiply the elements which are smaller than 5 and larger than 90 with 10 and the other elements with 0.1.
MATLAB: An Introduction with Applications
6th Edition
ISBN:9781119256830
Author:Amos Gilat
Publisher:Amos Gilat
Chapter1: Starting With Matlab
Section: Chapter Questions
Problem 1P
Related questions
Question
![2:34 PMⒸ
6.2. CONDITIONAL STATEMENTS
STATISTICAL PROGRAMMING I
Lab Exercise: #1
DO
DO
Tools
..
1. Make a vector from 1 to 100. Make a for-loop which runs through the whole vector. Multiply
the elements which are smaller than 5 and larger than 90 with 10 and the other elements
with 0.1.
(a) for (i in 1:2) print (i)
(b) for (i in (1:4)-2) print (i)
2. Write a short program that reports the absolute value of every element in a vector, v. Hint:
the number of elements in a vector v can be obtained by length (v)
3. Write a short program that reports "PASS" for exam marks >= 40 or "FAIL" for exam marks
< 40. Put the exam marks in a vector called "exam".
4. Write a program using a for loop to sum the integers 1 to 10.
5. Write some lines of R code that will calculate the sum 1+2+3+...+300, using a for loop.
6. Write an R code to compute the factorial for an integer x.
7. Write an R code using a for loop for printing the square root of the integers one to 100.
8. The following R codes might be having some error(s). Correct the error(s) and write the
expected output of the following R code:
(c) a 1:10
b=NULL
for(i in seq(along=a))
{ if (a[i]<5 || a[i]>8} {b=c(b, a[i]) } else {
stop ("values need to be <5") } }
(e) rm(list())
a 1:10
(d) rm(list())
a=1:10
b=NULL
for (i in seq(along=a))
{ if (a[i]<5|| a[i]>8} {b-c (b, a[i]) } else
{ break ("values need to be <5") } }
.ıll.
(f) y=log (c(3, 0.5, 2, 4))
ifelse (y<0, NA, y)
b=NULL
for (i in seq(along=a))
{ if (a[i]<5 || a[i]>8} {b=c(b, a[i]) } {else{next}}
b
• if statement
• warning messages
6.2. CONDITIONAL STATEMENTS
49
9. A geometric progression is defined by the recursive relation an = ran-1. Write an R
program that gives the geometric progression. Don't forget to use:
Mobile View
10. Use repeat, next and break to print the odd integers between 1 and 10. Re
exercise using while, instead of repeat, and print the even integers.
11. Generate the following four vectors of length 100.
(a) For each column, the average of all 1,000 observations in that column.
18
Share
W
PDF to DOC
77
x
心
Edit on PC](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F17c87b98-4798-4a02-b940-27cdfaf993a3%2Fb3824be2-19ad-4656-a97f-a82896c433e8%2Fuam3bef_processed.jpeg&w=3840&q=75)
Transcribed Image Text:2:34 PMⒸ
6.2. CONDITIONAL STATEMENTS
STATISTICAL PROGRAMMING I
Lab Exercise: #1
DO
DO
Tools
..
1. Make a vector from 1 to 100. Make a for-loop which runs through the whole vector. Multiply
the elements which are smaller than 5 and larger than 90 with 10 and the other elements
with 0.1.
(a) for (i in 1:2) print (i)
(b) for (i in (1:4)-2) print (i)
2. Write a short program that reports the absolute value of every element in a vector, v. Hint:
the number of elements in a vector v can be obtained by length (v)
3. Write a short program that reports "PASS" for exam marks >= 40 or "FAIL" for exam marks
< 40. Put the exam marks in a vector called "exam".
4. Write a program using a for loop to sum the integers 1 to 10.
5. Write some lines of R code that will calculate the sum 1+2+3+...+300, using a for loop.
6. Write an R code to compute the factorial for an integer x.
7. Write an R code using a for loop for printing the square root of the integers one to 100.
8. The following R codes might be having some error(s). Correct the error(s) and write the
expected output of the following R code:
(c) a 1:10
b=NULL
for(i in seq(along=a))
{ if (a[i]<5 || a[i]>8} {b=c(b, a[i]) } else {
stop ("values need to be <5") } }
(e) rm(list())
a 1:10
(d) rm(list())
a=1:10
b=NULL
for (i in seq(along=a))
{ if (a[i]<5|| a[i]>8} {b-c (b, a[i]) } else
{ break ("values need to be <5") } }
.ıll.
(f) y=log (c(3, 0.5, 2, 4))
ifelse (y<0, NA, y)
b=NULL
for (i in seq(along=a))
{ if (a[i]<5 || a[i]>8} {b=c(b, a[i]) } {else{next}}
b
• if statement
• warning messages
6.2. CONDITIONAL STATEMENTS
49
9. A geometric progression is defined by the recursive relation an = ran-1. Write an R
program that gives the geometric progression. Don't forget to use:
Mobile View
10. Use repeat, next and break to print the odd integers between 1 and 10. Re
exercise using while, instead of repeat, and print the even integers.
11. Generate the following four vectors of length 100.
(a) For each column, the average of all 1,000 observations in that column.
18
Share
W
PDF to DOC
77
x
心
Edit on PC
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images

Recommended textbooks for you

MATLAB: An Introduction with Applications
Statistics
ISBN:
9781119256830
Author:
Amos Gilat
Publisher:
John Wiley & Sons Inc

Probability and Statistics for Engineering and th…
Statistics
ISBN:
9781305251809
Author:
Jay L. Devore
Publisher:
Cengage Learning

Statistics for The Behavioral Sciences (MindTap C…
Statistics
ISBN:
9781305504912
Author:
Frederick J Gravetter, Larry B. Wallnau
Publisher:
Cengage Learning

MATLAB: An Introduction with Applications
Statistics
ISBN:
9781119256830
Author:
Amos Gilat
Publisher:
John Wiley & Sons Inc

Probability and Statistics for Engineering and th…
Statistics
ISBN:
9781305251809
Author:
Jay L. Devore
Publisher:
Cengage Learning

Statistics for The Behavioral Sciences (MindTap C…
Statistics
ISBN:
9781305504912
Author:
Frederick J Gravetter, Larry B. Wallnau
Publisher:
Cengage Learning

Elementary Statistics: Picturing the World (7th E…
Statistics
ISBN:
9780134683416
Author:
Ron Larson, Betsy Farber
Publisher:
PEARSON

The Basic Practice of Statistics
Statistics
ISBN:
9781319042578
Author:
David S. Moore, William I. Notz, Michael A. Fligner
Publisher:
W. H. Freeman

Introduction to the Practice of Statistics
Statistics
ISBN:
9781319013387
Author:
David S. Moore, George P. McCabe, Bruce A. Craig
Publisher:
W. H. Freeman