{
"cells": [
{
"cell_type": "markdown",
"id": "29fa438f-a7c8-4580-adf0-9bab32d65e38",
"metadata": {
"tags": []
},
"source": [
"# Basic Operation "
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3be36216-40d3-4606-bbcf-799784f84c09",
"metadata": {},
"outputs": [],
"source": [
"X = 34\n",
"Y = 3"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d52603a5-e2b6-4f31-adfc-d074dfdfce12",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"37"
],
"text/latex": [
"37"
],
"text/markdown": [
"37"
],
"text/plain": [
"[1] 37"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"31"
],
"text/latex": [
"31"
],
"text/markdown": [
"31"
],
"text/plain": [
"[1] 31"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"102"
],
"text/latex": [
"102"
],
"text/markdown": [
"102"
],
"text/plain": [
"[1] 102"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"39304"
],
"text/latex": [
"39304"
],
"text/markdown": [
"39304"
],
"text/plain": [
"[1] 39304"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"1"
],
"text/latex": [
"1"
],
"text/markdown": [
"1"
],
"text/plain": [
"[1] 1"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"'numeric'"
],
"text/latex": [
"'numeric'"
],
"text/markdown": [
"'numeric'"
],
"text/plain": [
"[1] \"numeric\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"X + Y\n",
"X - Y\n",
"X * Y\n",
"X ^ Y\n",
"X %% Y\n",
"class(X)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "ebddaf46-4bbe-4b81-93ea-87962d791a23",
"metadata": {},
"outputs": [],
"source": [
"A = 'Helsinki'\n",
"B = 'Warsaw'\n",
"C = 'Stockholm'\n",
"D = 'Budapest'"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "2d10f923-0b09-4305-ac50-05a1f80bba86",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"'character'"
],
"text/latex": [
"'character'"
],
"text/markdown": [
"'character'"
],
"text/plain": [
"[1] \"character\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"class(A)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1b4ac946-70dc-455d-882d-e25863beb9aa",
"metadata": {},
"outputs": [],
"source": [
"v <- c(3, 6, 9, 15)\n",
"w <- c(4, 6, 8, 12)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "6f679994-f3eb-44dc-b461-0317e01edf1a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\t- 7
\n",
"\t- 12
\n",
"\t- 17
\n",
"\t- 27
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 7\n",
"\\item 12\n",
"\\item 17\n",
"\\item 27\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 7\n",
"2. 12\n",
"3. 17\n",
"4. 27\n",
"\n",
"\n"
],
"text/plain": [
"[1] 7 12 17 27"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"v + w"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "e293301e-301f-43a3-b49d-172fe368142d",
"metadata": {},
"outputs": [],
"source": [
"cities <- c(A, B, C, D)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "50410406-4db5-4caf-ac62-8cccb86e959f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 'Helsinki'
\n",
"\t- 'Warsaw'
\n",
"\t- 'Stockholm'
\n",
"\t- 'Budapest'
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 'Helsinki'\n",
"\\item 'Warsaw'\n",
"\\item 'Stockholm'\n",
"\\item 'Budapest'\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 'Helsinki'\n",
"2. 'Warsaw'\n",
"3. 'Stockholm'\n",
"4. 'Budapest'\n",
"\n",
"\n"
],
"text/plain": [
"[1] \"Helsinki\" \"Warsaw\" \"Stockholm\" \"Budapest\" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cities"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "33bd4e44-39ed-4b7a-b587-45aaa6c24a1d",
"metadata": {},
"outputs": [],
"source": [
"names(w) <- c(A, B, C, D)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "065ae4bd-094c-4503-9995-7192fb2656ec",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- Helsinki
\n",
"\t\t- 4
\n",
"\t- Warsaw
\n",
"\t\t- 6
\n",
"\t- Stockholm
\n",
"\t\t- 8
\n",
"\t- Budapest
\n",
"\t\t- 12
\n",
"
\n"
],
"text/latex": [
"\\begin{description*}\n",
"\\item[Helsinki] 4\n",
"\\item[Warsaw] 6\n",
"\\item[Stockholm] 8\n",
"\\item[Budapest] 12\n",
"\\end{description*}\n"
],
"text/markdown": [
"Helsinki\n",
": 4Warsaw\n",
": 6Stockholm\n",
": 8Budapest\n",
": 12\n",
"\n"
],
"text/plain": [
" Helsinki Warsaw Stockholm Budapest \n",
" 4 6 8 12 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"w"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "7c75a588-a361-4cba-8a04-5cba95275571",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"Stockholm: 8"
],
"text/latex": [
"\\textbf{Stockholm:} 8"
],
"text/markdown": [
"**Stockholm:** 8"
],
"text/plain": [
"Stockholm \n",
" 8 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"w[3]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "056a36fa-1b81-49b0-9522-b5c6eae2ab03",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"Stockholm: 8"
],
"text/latex": [
"\\textbf{Stockholm:} 8"
],
"text/markdown": [
"**Stockholm:** 8"
],
"text/plain": [
"Stockholm \n",
" 8 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"w['Stockholm']"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "543e42f4-c98a-42c2-a008-efcd6170f372",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- Warsaw
\n",
"\t\t- 6
\n",
"\t- Stockholm
\n",
"\t\t- 8
\n",
"
\n"
],
"text/latex": [
"\\begin{description*}\n",
"\\item[Warsaw] 6\n",
"\\item[Stockholm] 8\n",
"\\end{description*}\n"
],
"text/markdown": [
"Warsaw\n",
": 6Stockholm\n",
": 8\n",
"\n"
],
"text/plain": [
" Warsaw Stockholm \n",
" 6 8 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"w[c(2, 3)]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "ec8d5861-f67c-4ee2-82f0-de2a403aeaae",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- Warsaw
\n",
"\t\t- 6
\n",
"\t- Stockholm
\n",
"\t\t- 8
\n",
"\t- Budapest
\n",
"\t\t- 12
\n",
"
\n"
],
"text/latex": [
"\\begin{description*}\n",
"\\item[Warsaw] 6\n",
"\\item[Stockholm] 8\n",
"\\item[Budapest] 12\n",
"\\end{description*}\n"
],
"text/markdown": [
"Warsaw\n",
": 6Stockholm\n",
": 8Budapest\n",
": 12\n",
"\n"
],
"text/plain": [
" Warsaw Stockholm Budapest \n",
" 6 8 12 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"w[c(2:4)]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "becba461-acae-4a64-af38-96fd936d698c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"7.5"
],
"text/latex": [
"7.5"
],
"text/markdown": [
"7.5"
],
"text/plain": [
"[1] 7.5"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mean(w)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "af50a066-e634-4f42-b661-7a2cf465c9f7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- Helsinki
\n",
"\t\t- FALSE
\n",
"\t- Warsaw
\n",
"\t\t- FALSE
\n",
"\t- Stockholm
\n",
"\t\t- TRUE
\n",
"\t- Budapest
\n",
"\t\t- TRUE
\n",
"
\n"
],
"text/latex": [
"\\begin{description*}\n",
"\\item[Helsinki] FALSE\n",
"\\item[Warsaw] FALSE\n",
"\\item[Stockholm] TRUE\n",
"\\item[Budapest] TRUE\n",
"\\end{description*}\n"
],
"text/markdown": [
"Helsinki\n",
": FALSEWarsaw\n",
": FALSEStockholm\n",
": TRUEBudapest\n",
": TRUE\n",
"\n"
],
"text/plain": [
" Helsinki Warsaw Stockholm Budapest \n",
" FALSE FALSE TRUE TRUE "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"w > 7"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "52dabd31-5d79-4804-8c4d-c1dbbbaa35b9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- Stockholm
\n",
"\t\t- 8
\n",
"\t- Budapest
\n",
"\t\t- 12
\n",
"
\n"
],
"text/latex": [
"\\begin{description*}\n",
"\\item[Stockholm] 8\n",
"\\item[Budapest] 12\n",
"\\end{description*}\n"
],
"text/markdown": [
"Stockholm\n",
": 8Budapest\n",
": 12\n",
"\n"
],
"text/plain": [
"Stockholm Budapest \n",
" 8 12 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"w[w>7]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "f331bcb8-a31f-41ec-88dc-f8d4999151f3",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\t1 | 2 | 3 | 4 |
\n",
"\t5 | 6 | 7 | 8 |
\n",
"\t9 | 10 | 11 | 12 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{llll}\n",
"\t 1 & 2 & 3 & 4\\\\\n",
"\t 5 & 6 & 7 & 8\\\\\n",
"\t 9 & 10 & 11 & 12\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| 1 | 2 | 3 | 4 |\n",
"| 5 | 6 | 7 | 8 |\n",
"| 9 | 10 | 11 | 12 |\n",
"\n"
],
"text/plain": [
" [,1] [,2] [,3] [,4]\n",
"[1,] 1 2 3 4 \n",
"[2,] 5 6 7 8 \n",
"[3,] 9 10 11 12 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"matrix(1:12, byrow=TRUE, nrow=3)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "2808eff5-93c5-434a-87d2-89163cb7979d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\t1 | 4 | 7 | 10 |
\n",
"\t2 | 5 | 8 | 11 |
\n",
"\t3 | 6 | 9 | 12 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{llll}\n",
"\t 1 & 4 & 7 & 10\\\\\n",
"\t 2 & 5 & 8 & 11\\\\\n",
"\t 3 & 6 & 9 & 12\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| 1 | 4 | 7 | 10 |\n",
"| 2 | 5 | 8 | 11 |\n",
"| 3 | 6 | 9 | 12 |\n",
"\n"
],
"text/plain": [
" [,1] [,2] [,3] [,4]\n",
"[1,] 1 4 7 10 \n",
"[2,] 2 5 8 11 \n",
"[3,] 3 6 9 12 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"matrix(1:12, byrow=FALSE, nrow=3)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "e0728cbe-e30a-4c40-988d-635bc544a47d",
"metadata": {},
"outputs": [],
"source": [
"v <- c(3, 6, 9, 15)\n",
"w <- c(4, 6, 8, 12)\n",
"v_w <- c(v, w)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "1d216e46-423e-49b5-ab6e-c430356159a9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 3
\n",
"\t- 6
\n",
"\t- 9
\n",
"\t- 15
\n",
"\t- 4
\n",
"\t- 6
\n",
"\t- 8
\n",
"\t- 12
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 3\n",
"\\item 6\n",
"\\item 9\n",
"\\item 15\n",
"\\item 4\n",
"\\item 6\n",
"\\item 8\n",
"\\item 12\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 3\n",
"2. 6\n",
"3. 9\n",
"4. 15\n",
"5. 4\n",
"6. 6\n",
"7. 8\n",
"8. 12\n",
"\n",
"\n"
],
"text/plain": [
"[1] 3 6 9 15 4 6 8 12"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"v_w"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "50728047-b6fc-432c-81bd-baad08c6c93d",
"metadata": {},
"outputs": [],
"source": [
"v_w_mat <- matrix(v_w, byrow=TRUE, nrow=4)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "8f59fa04-8eba-4ef0-a853-95ae8567e308",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\t3 | 6 |
\n",
"\t9 | 15 |
\n",
"\t4 | 6 |
\n",
"\t8 | 12 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{ll}\n",
"\t 3 & 6\\\\\n",
"\t 9 & 15\\\\\n",
"\t 4 & 6\\\\\n",
"\t 8 & 12\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| 3 | 6 |\n",
"| 9 | 15 |\n",
"| 4 | 6 |\n",
"| 8 | 12 |\n",
"\n"
],
"text/plain": [
" [,1] [,2]\n",
"[1,] 3 6 \n",
"[2,] 9 15 \n",
"[3,] 4 6 \n",
"[4,] 8 12 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"v_w_mat"
]
},
{
"cell_type": "markdown",
"id": "f75de245-fb4d-4a06-877e-bc81daf16656",
"metadata": {},
"source": [
"## Naming Matrix"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "510f9119-164c-4203-b9af-dcf2e92d324c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" [,1] [,2] [,3]\n",
"[1,] 1 2 3\n",
"[2,] 4 5 6\n",
"[3,] 7 8 9\n"
]
}
],
"source": [
"mat_A <- matrix(1:9, byrow=TRUE, nrow=3)\n",
"print(mat_A)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "81059de3-d28b-4b9a-bd45-ac0988e71d24",
"metadata": {},
"outputs": [],
"source": [
"rownames(mat_A) <- c('Row 1', 'Row 2', 'Row 3')\n",
"colnames(mat_A) <- c('Col 1', 'Col 2', 'Col 3')"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "69127e58-cb23-4b68-ba26-cf5a658a79ad",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Col 1 Col 2 Col 3\n",
"Row 1 1 2 3\n",
"Row 2 4 5 6\n",
"Row 3 7 8 9\n"
]
}
],
"source": [
"print(mat_A)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "0e089ff3-062a-4517-b34b-c713a5a7a4c3",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- Row 1
\n",
"\t\t- 6
\n",
"\t- Row 2
\n",
"\t\t- 15
\n",
"\t- Row 3
\n",
"\t\t- 24
\n",
"
\n"
],
"text/latex": [
"\\begin{description*}\n",
"\\item[Row 1] 6\n",
"\\item[Row 2] 15\n",
"\\item[Row 3] 24\n",
"\\end{description*}\n"
],
"text/markdown": [
"Row 1\n",
": 6Row 2\n",
": 15Row 3\n",
": 24\n",
"\n"
],
"text/plain": [
"Row 1 Row 2 Row 3 \n",
" 6 15 24 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rowSums(mat_A)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "897976d1-d9af-4745-9be5-283b87a46b4c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- Col 1
\n",
"\t\t- 12
\n",
"\t- Col 2
\n",
"\t\t- 15
\n",
"\t- Col 3
\n",
"\t\t- 18
\n",
"
\n"
],
"text/latex": [
"\\begin{description*}\n",
"\\item[Col 1] 12\n",
"\\item[Col 2] 15\n",
"\\item[Col 3] 18\n",
"\\end{description*}\n"
],
"text/markdown": [
"Col 1\n",
": 12Col 2\n",
": 15Col 3\n",
": 18\n",
"\n"
],
"text/plain": [
"Col 1 Col 2 Col 3 \n",
" 12 15 18 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"colSums(mat_A)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "123aa4f1-a791-422f-8368-c08df8c19675",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\t2 | 5 | 8 |
\n",
"\t3 | 6 | 9 |
\n",
"\t4 | 7 | 10 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{lll}\n",
"\t 2 & 5 & 8\\\\\n",
"\t 3 & 6 & 9\\\\\n",
"\t 4 & 7 & 10\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| 2 | 5 | 8 |\n",
"| 3 | 6 | 9 |\n",
"| 4 | 7 | 10 |\n",
"\n"
],
"text/plain": [
" [,1] [,2] [,3]\n",
"[1,] 2 5 8 \n",
"[2,] 3 6 9 \n",
"[3,] 4 7 10 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mat_B = matrix(2:10, byrow = FALSE, nrow = 3)\n",
"mat_B"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "b94e7ed0-8e61-46f2-b949-54c5376400b9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | Col 1 | Col 2 | Col 3 |
\n",
"\n",
"\tRow 1 | 3 | 7 | 11 |
\n",
"\tRow 2 | 7 | 11 | 15 |
\n",
"\tRow 3 | 11 | 15 | 19 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lll}\n",
" & Col 1 & Col 2 & Col 3\\\\\n",
"\\hline\n",
"\tRow 1 & 3 & 7 & 11\\\\\n",
"\tRow 2 & 7 & 11 & 15\\\\\n",
"\tRow 3 & 11 & 15 & 19\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | Col 1 | Col 2 | Col 3 |\n",
"|---|---|---|---|\n",
"| Row 1 | 3 | 7 | 11 |\n",
"| Row 2 | 7 | 11 | 15 |\n",
"| Row 3 | 11 | 15 | 19 |\n",
"\n"
],
"text/plain": [
" Col 1 Col 2 Col 3\n",
"Row 1 3 7 11 \n",
"Row 2 7 11 15 \n",
"Row 3 11 15 19 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mat_A + mat_B"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "2cff5dc0-f06a-4218-84ec-40e0a51388be",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | Col 1 | Col 2 | Col 3 |
\n",
"\n",
"\tRow 1 | 2 | 10 | 24 |
\n",
"\tRow 2 | 12 | 30 | 54 |
\n",
"\tRow 3 | 28 | 56 | 90 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lll}\n",
" & Col 1 & Col 2 & Col 3\\\\\n",
"\\hline\n",
"\tRow 1 & 2 & 10 & 24\\\\\n",
"\tRow 2 & 12 & 30 & 54\\\\\n",
"\tRow 3 & 28 & 56 & 90\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | Col 1 | Col 2 | Col 3 |\n",
"|---|---|---|---|\n",
"| Row 1 | 2 | 10 | 24 |\n",
"| Row 2 | 12 | 30 | 54 |\n",
"| Row 3 | 28 | 56 | 90 |\n",
"\n"
],
"text/plain": [
" Col 1 Col 2 Col 3\n",
"Row 1 2 10 24 \n",
"Row 2 12 30 54 \n",
"Row 3 28 56 90 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mat_A * mat_B"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "54212886-0bd5-4854-8bf8-baba0d5f4ad0",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | Col 1 | Col 2 | Col 3 |
\n",
"\n",
"\tRow 1 | 0.500000 | 0.4000000 | 0.3750000 |
\n",
"\tRow 2 | 1.333333 | 0.8333333 | 0.6666667 |
\n",
"\tRow 3 | 1.750000 | 1.1428571 | 0.9000000 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lll}\n",
" & Col 1 & Col 2 & Col 3\\\\\n",
"\\hline\n",
"\tRow 1 & 0.500000 & 0.4000000 & 0.3750000\\\\\n",
"\tRow 2 & 1.333333 & 0.8333333 & 0.6666667\\\\\n",
"\tRow 3 & 1.750000 & 1.1428571 & 0.9000000\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | Col 1 | Col 2 | Col 3 |\n",
"|---|---|---|---|\n",
"| Row 1 | 0.500000 | 0.4000000 | 0.3750000 |\n",
"| Row 2 | 1.333333 | 0.8333333 | 0.6666667 |\n",
"| Row 3 | 1.750000 | 1.1428571 | 0.9000000 |\n",
"\n"
],
"text/plain": [
" Col 1 Col 2 Col 3 \n",
"Row 1 0.500000 0.4000000 0.3750000\n",
"Row 2 1.333333 0.8333333 0.6666667\n",
"Row 3 1.750000 1.1428571 0.9000000"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mat_A / mat_B"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ba4349aa-94ab-4558-9fe1-122c6b2d1dc0",
"metadata": {},
"outputs": [],
"source": [
"mat_A %*% mat_B # matrix multiplication"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "09cc1f37-ef13-402f-92eb-71610bce22b2",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | Col 1 | Col 2 | Col 3 | | | |
\n",
"\n",
"\tRow 1 | 1 | 2 | 3 | 2 | 5 | 8 |
\n",
"\tRow 2 | 4 | 5 | 6 | 3 | 6 | 9 |
\n",
"\tRow 3 | 7 | 8 | 9 | 4 | 7 | 10 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|llllll}\n",
" & Col 1 & Col 2 & Col 3 & & & \\\\\n",
"\\hline\n",
"\tRow 1 & 1 & 2 & 3 & 2 & 5 & 8\\\\\n",
"\tRow 2 & 4 & 5 & 6 & 3 & 6 & 9\\\\\n",
"\tRow 3 & 7 & 8 & 9 & 4 & 7 & 10\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | Col 1 | Col 2 | Col 3 | | | |\n",
"|---|---|---|---|---|---|---|\n",
"| Row 1 | 1 | 2 | 3 | 2 | 5 | 8 |\n",
"| Row 2 | 4 | 5 | 6 | 3 | 6 | 9 |\n",
"| Row 3 | 7 | 8 | 9 | 4 | 7 | 10 |\n",
"\n"
],
"text/plain": [
" Col 1 Col 2 Col 3 \n",
"Row 1 1 2 3 2 5 8\n",
"Row 2 4 5 6 3 6 9\n",
"Row 3 7 8 9 4 7 10"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cbind(mat_A, mat_B)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "67c6b09e-0ae7-4f84-84b2-19cb0ea45972",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | Col 1 | Col 2 | Col 3 |
\n",
"\n",
"\tRow 1 | 1 | 2 | 3 |
\n",
"\tRow 2 | 4 | 5 | 6 |
\n",
"\tRow 3 | 7 | 8 | 9 |
\n",
"\t | 2 | 5 | 8 |
\n",
"\t | 3 | 6 | 9 |
\n",
"\t | 4 | 7 | 10 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lll}\n",
" & Col 1 & Col 2 & Col 3\\\\\n",
"\\hline\n",
"\tRow 1 & 1 & 2 & 3\\\\\n",
"\tRow 2 & 4 & 5 & 6\\\\\n",
"\tRow 3 & 7 & 8 & 9\\\\\n",
"\t & 2 & 5 & 8\\\\\n",
"\t & 3 & 6 & 9\\\\\n",
"\t & 4 & 7 & 10\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | Col 1 | Col 2 | Col 3 |\n",
"|---|---|---|---|\n",
"| Row 1 | 1 | 2 | 3 |\n",
"| Row 2 | 4 | 5 | 6 |\n",
"| Row 3 | 7 | 8 | 9 |\n",
"| | 2 | 5 | 8 |\n",
"| | 3 | 6 | 9 |\n",
"| | 4 | 7 | 10 |\n",
"\n"
],
"text/plain": [
" Col 1 Col 2 Col 3\n",
"Row 1 1 2 3 \n",
"Row 2 4 5 6 \n",
"Row 3 7 8 9 \n",
" 2 5 8 \n",
" 3 6 9 \n",
" 4 7 10 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rbind(mat_A, mat_B)"
]
},
{
"cell_type": "markdown",
"id": "405cb0be-a968-416c-9581-2ad9850ab42a",
"metadata": {},
"source": [
"## Check Workspace"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "9cc25907-676e-4241-b4a1-9c6b380c4839",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 'A'
\n",
"\t- 'B'
\n",
"\t- 'C'
\n",
"\t- 'cities'
\n",
"\t- 'D'
\n",
"\t- 'mat_A'
\n",
"\t- 'mat_B'
\n",
"\t- 'v'
\n",
"\t- 'v_w'
\n",
"\t- 'v_w_mat'
\n",
"\t- 'vector_a'
\n",
"\t- 'vector_b'
\n",
"\t- 'w'
\n",
"\t- 'X'
\n",
"\t- 'Y'
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 'A'\n",
"\\item 'B'\n",
"\\item 'C'\n",
"\\item 'cities'\n",
"\\item 'D'\n",
"\\item 'mat\\_A'\n",
"\\item 'mat\\_B'\n",
"\\item 'v'\n",
"\\item 'v\\_w'\n",
"\\item 'v\\_w\\_mat'\n",
"\\item 'vector\\_a'\n",
"\\item 'vector\\_b'\n",
"\\item 'w'\n",
"\\item 'X'\n",
"\\item 'Y'\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 'A'\n",
"2. 'B'\n",
"3. 'C'\n",
"4. 'cities'\n",
"5. 'D'\n",
"6. 'mat_A'\n",
"7. 'mat_B'\n",
"8. 'v'\n",
"9. 'v_w'\n",
"10. 'v_w_mat'\n",
"11. 'vector_a'\n",
"12. 'vector_b'\n",
"13. 'w'\n",
"14. 'X'\n",
"15. 'Y'\n",
"\n",
"\n"
],
"text/plain": [
" [1] \"A\" \"B\" \"C\" \"cities\" \"D\" \"mat_A\" \n",
" [7] \"mat_B\" \"v\" \"v_w\" \"v_w_mat\" \"vector_a\" \"vector_b\"\n",
"[13] \"w\" \"X\" \"Y\" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ls()"
]
},
{
"cell_type": "markdown",
"id": "416f4cf9-ebf8-4ba1-b413-4447b5d39224",
"metadata": {},
"source": [
"# Select Matrix Element"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "5446649e-a534-4f6a-adb4-5ee597a6c355",
"metadata": {},
"outputs": [],
"source": [
"mat_C = matrix(1:30, byrow=TRUE, nrow=5)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "a3c921fd-aac9-4fde-8fb5-10625237c5c4",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\t 1 | 2 | 3 | 4 | 5 | 6 |
\n",
"\t 7 | 8 | 9 | 10 | 11 | 12 |
\n",
"\t13 | 14 | 15 | 16 | 17 | 18 |
\n",
"\t19 | 20 | 21 | 22 | 23 | 24 |
\n",
"\t25 | 26 | 27 | 28 | 29 | 30 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{llllll}\n",
"\t 1 & 2 & 3 & 4 & 5 & 6\\\\\n",
"\t 7 & 8 & 9 & 10 & 11 & 12\\\\\n",
"\t 13 & 14 & 15 & 16 & 17 & 18\\\\\n",
"\t 19 & 20 & 21 & 22 & 23 & 24\\\\\n",
"\t 25 & 26 & 27 & 28 & 29 & 30\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| 1 | 2 | 3 | 4 | 5 | 6 |\n",
"| 7 | 8 | 9 | 10 | 11 | 12 |\n",
"| 13 | 14 | 15 | 16 | 17 | 18 |\n",
"| 19 | 20 | 21 | 22 | 23 | 24 |\n",
"| 25 | 26 | 27 | 28 | 29 | 30 |\n",
"\n"
],
"text/plain": [
" [,1] [,2] [,3] [,4] [,5] [,6]\n",
"[1,] 1 2 3 4 5 6 \n",
"[2,] 7 8 9 10 11 12 \n",
"[3,] 13 14 15 16 17 18 \n",
"[4,] 19 20 21 22 23 24 \n",
"[5,] 25 26 27 28 29 30 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mat_C"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "42dddb60-26b1-4e2e-bab3-a5e01b147c4b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 5
\n",
"\t- 11
\n",
"\t- 17
\n",
"\t- 23
\n",
"\t- 29
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 5\n",
"\\item 11\n",
"\\item 17\n",
"\\item 23\n",
"\\item 29\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 5\n",
"2. 11\n",
"3. 17\n",
"4. 23\n",
"5. 29\n",
"\n",
"\n"
],
"text/plain": [
"[1] 5 11 17 23 29"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mat_C[,5]"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "7b1ac75d-a076-4ea5-aeb2-a7104f011c6f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 13
\n",
"\t- 14
\n",
"\t- 15
\n",
"\t- 16
\n",
"\t- 17
\n",
"\t- 18
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 13\n",
"\\item 14\n",
"\\item 15\n",
"\\item 16\n",
"\\item 17\n",
"\\item 18\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 13\n",
"2. 14\n",
"3. 15\n",
"4. 16\n",
"5. 17\n",
"6. 18\n",
"\n",
"\n"
],
"text/plain": [
"[1] 13 14 15 16 17 18"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mat_C[3,]"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "17f04a84-6982-48e3-a819-97976d17c2ee",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 10
\n",
"\t- 16
\n",
"\t- 22
\n",
"\t- 28
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 10\n",
"\\item 16\n",
"\\item 22\n",
"\\item 28\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 10\n",
"2. 16\n",
"3. 22\n",
"4. 28\n",
"\n",
"\n"
],
"text/plain": [
"[1] 10 16 22 28"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mat_C[2:5,4]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ed304cf6-a3f4-4ec0-9bf7-862f507f185e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}