Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

MATLAB offers many built-in functions to allow for easier importing and exporting of files. In this assignment, students will see how these pre-written functions can

MATLAB offers many built-in functions to allow for easier importing and exporting of files. In this assignment, students will see how these pre-written functions can make certain programming tasks much less onerous.

This assignment uses a rubric. Please review the rubric prior to beginning the assignment to become familiar with the expectations for successful completion.

Refer to "CST-211 Technical Report" to complete this assignment.

You will write a program in MATLAB that multiplies a matrix by a vector, using your old C program to check the results. Your program must ask the user for the input vector file and input matrix file names. Output will be written to 'output.csv'.

Create a program in MATLAB to solve the Topic 5 "File I/O" assignment. Use any built-in MATLAB functions of your choice. Some might require you to change your input files slightly; if so, document what you did and explain why. Compare and contrast the C and MATLAB versions of your code.

This is my old C program, the assignment is basically asking to convert my old C program into a MATLAB program:

#include #include #include

int main() { FILE * inputMatrix, * inputVector, * outputResult; //Declare FILE variables float **matrixValues, *vectorValues, *resultantVect; //Declare the pointers int matRow=0, matCol=0, vectRow=0, i = 0, j=0; //Define and declare the int variables char matFileName[15], vectFileName[15];//string variables defined, utilized to read file name

printf("\t\tMatrix Vector Multiplication "); //prints the main purpose of the program

printf("Enter Matrix file name:");//user input file name scanf("%s", matFileName);

printf("Enter Vector file name:");//user input vector file name scanf("%s", vectFileName);

inputMatrix = fopen(matFileName, "r");//matrix file opened in read mode if(inputMatrix == NULL)//if file inputted does not exist, give out an error message { printf("Unable to obtain the file %s ", matFileName); return 1; }

inputVector = fopen(vectFileName, "r");//vector file opened in read mode if(inputVector == NULL)//display an error message if file does not exist { printf("Sorry! Unable to open the file %s ", vectFileName); return 1; }

fscanf(inputMatrix, "%d", &matRow); fscanf(inputMatrix, "%d", &matCol);//fscanf reads row and column values from inputMatrix file matrixValues = (float **)malloc(sizeof(float *) * matRow);//memory dynamically allocated for matrixValues

// allocate memory for each row value of matrixValues for(i = 0; i < matRow; i++) matrixValues[i] = (float *)malloc(sizeof(float) * matCol);//allocates memory for each row value for(i = 0; i < matRow; i++) { for(j = 0; j < matCol ; j++) { fscanf(inputMatrix, "%f,", &matrixValues[i][j]); //values from file are read and stored in matrixValues } }

fclose(inputMatrix); //inputMatrix file closed

// display the matrix values printf(" The values in the matrix are: "); for(i = 0; i < matRow; i++) { for(j = 0; j < matCol - 1; j++) printf("%f\t", matrixValues[i][j]); //displays matrix values printf(" "); }

fscanf(inputVector, "%d", &vectRow); //reads vectRow value from inputVector file

vectorValues = (float *)malloc(vectRow * sizeof(float)); //memory for vector dynamically allocated

for(i = 0; i < vectRow; i++) { fscanf(inputVector, "%f", &vectorValues[i]); //values from inputVector file read }

fclose(inputVector); //inputVector file closed

printf(" The values in the vector are: "); for(i = 0; i < vectRow; i++) { printf("%f ", vectorValues[i]); //Vector values displayed } if(matCol != vectRow) //if matCol and vectRow are not equal,error message is displayed, and application is exited. { printf("Sorry! Cannot perform matrix_vector multiplication "); return 1; }

resultantVect = (float *)malloc(matRow * sizeof(float)); //memory allocated for resulatant vector

// loop through each row of the matrix for(i = 0; i < matRow; i++)//loops through each row of matrix { resultantVect[i] = 0; //resultantVect at i is set to 0

for(j = 0; j < matCol; j++) //loops through each column of matrix { resultantVect[i] = resultantVect[i] + matrixValues[i][j] * vectorValues[j]; //equation that calculates multiplication of matrices } }

outputResult = fopen("output.csv", "wb"); //output.csv file is opened

fprintf(outputResult,"%d ", matRow); //row result values is displayed

printf(" Resultant matrix_vector multiplication is: "); //displays matrix result in output file

for(i = 0; i < matRow; i++) //matRow loop { printf("%f ", resultantVect[i]); //resultant value is displayed

fprintf(outputResult, "%f ", resultantVect[i]); //resultant value displayed in output file fflush(outputResult); }

fclose(outputResult); //closes outputResult file

getchar(); getchar(); //holds the screen return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analytics Systems Engineering Cybersecurity Project Management

Authors: Christopher Greco

1st Edition

168392648X, 978-1683926481

More Books

Students also viewed these Databases questions

Question

17. Explain some strategies for dealing with confl ict.

Answered: 1 week ago