Question

You will write a program in C that multiplies a matrix by a vector, using MATLAB...

You will write a program in C that multiplies a matrix by a vector, using MATLAB to check the results. 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. works in matlab

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in C

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<stdbool.h>


int main(){

    int mat[3][3]={{1,2,3},{4,5,6},{7,8,9}};

    int m=3, n=3;

    int vec[3][1]={{5},{1},{2}};

    

    printf("Matrix\n");

    for(int i=0;i<3;i++){

        for(int j=0;j<3;j++)

            printf("%d ",mat[i][j]);

        printf("\n");

    }

    printf("\nVector:\n");

    for(int i=0;i<3;i++){

        printf("%d \n",vec[i][0]);

    }

    printf("\nProduct\n");

    for(int i=0;i<m;i++){

        int sum=0;

        for(int j=0;j<n;j++)

            sum+=mat[i][j]*vec[j][0];

        printf("%d \n",sum);

    }

}

Same Code in matlab

---------------------------------------------------------------------------------------------------------

contrast: No loops in matlab, 3 lines of code. No printf statement.

File IO in matlab

(text file)

Code

x = 0:0.1:pi;
s = sin(x);
A = [x;s];
fID = fopen('sinx.txt','w');
fprintf(fID,'%6s %12s\r\n','x','sin(x)');
fprintf(fID,'%6.2f %12.8f\r\n',A);
fclose(fID);

this code generate table of sin(x) in a text file called sinx.txt

-----------------------

Add a comment
Know the answer?
Add Answer to:
You will write a program in C that multiplies a matrix by a vector, using MATLAB...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will write a program in C that multiplies a matrix by a vector, using MATLAB...

    You will write a program in C that multiplies a matrix by a vector, using MATLAB to check the results. 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.

  • Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt...

    Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt and multiplies them together to obtain matrix C such that the element C(i,j) ofmatrix C is given by: C(i, j) =  ∑A(i, k) ∗ B(k, j) where n = number of columns in A = number of rows in B. Display the matrix C in defaultMATLAB format.
You cannot use the MATLAB matrix multiply or other inbuilt MATLAB functions forarithmetic operations. You must implement it....

  • In Matlab Using the MATLAB built-in functions (zeros, ones, eyes), write a MATLAB program to create...

    In Matlab Using the MATLAB built-in functions (zeros, ones, eyes), write a MATLAB program to create the following matrix: 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 1 1 0 0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 0 1

  • PLEASE USE MATLAB ONLY PLEASE!! Modify MYSOLVER.m to make sure the inputs are valid. Your function...

    PLEASE USE MATLAB ONLY PLEASE!! Modify MYSOLVER.m to make sure the inputs are valid. Your function should checlk for each of the following cases: 1. A is not a square matrix; 2. b is not a column vector; 3. Ax and b do not have the same dimension. Submit your m-file and a diary showing how you tested the code. Only submit the m-file for MYSOLVER.m. Do not submit the m-files for backward.m. forward.m, or MYLU.m. Test to show that...

  • 9. (12 points) Using the MATLAB built-in functions (zeros, ones, eyes), write a MATLAB program to...

    9. (12 points) Using the MATLAB built-in functions (zeros, ones, eyes), write a MATLAB program to create the following matrix: 1 0 0 0 0 0 0 0 0 1 1 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 1 1 1 0 0 0 1 1 I

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • Publish using a MatLab function for the following: If a matrix A has dimension n×n and has n line...

    Publish using a MatLab function for the following: If a matrix A has dimension n×n and has n linearly independent eigenvectors, it is diagonalizable.This means there exists a matrix P such that P^(−1)AP=D, where D is a diagonal matrix whose diagonal entries are made up of the eigenvalues of A. P is constructed by taking the eigenvectors of A and using them as the columns of P. Your task is to write a program (function) that does the following If...

  • Nay programming languge is fine (MatLab, Octave, etc.) This is the whole document this last picture...

    Nay programming languge is fine (MatLab, Octave, etc.) This is the whole document this last picture i sent is the first page 5. Write and save a function Sphere Area that accepts Radius as input argument, and returns SurfaceArea (4 tt r) as output argument. Paste the function code below. Paste code here 6. Validate your Sphere Area function from the command line, checking the results against hand calculations. Paste the command line code and results below. 7. Design an...

  • DO THIS IN MATLAB PLEASE DO THIS IN MATLAB Create a script file that performs the...

    DO THIS IN MATLAB PLEASE DO THIS IN MATLAB Create a script file that performs the following calculations. Your script will use the functions you create. Label all graphs appropriately. For this project, do not have your homemade functions fprintf anything, instead have all your fprintf commands within your script. Attach your published script file along with .m files for your functions. Exercise 1. A fundamental iterative method for finding the roots of equations of one-variable is known as Newton's...

  • Written in C++ language Write a program that keeps track of a telephone directory. The program...

    Written in C++ language Write a program that keeps track of a telephone directory. The program should create a structure, Person, with the following data: struct Person { string firstName; string lastName; string phoneNumber; }; The program should then create a vector of Person structs, called directory. Your program should have functions to: - Read the vector data from a data file “telephoneData.txt” into the vector directory. - Print the details of all Persons in the vector. You may use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT