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...

  • MATLAB Your assignment for the week is to create "a MATLAB Exam Question". I want you...

    MATLAB Your assignment for the week is to create "a MATLAB Exam Question". I want you to create a multiple choice question. Once you have created a question, you also need to create 4-5 choices for the question, with one answer being correct. Very simple questions are not allowed, Anyone in the course can answer the question (no extra hard or not included topics) It should cover one of the topics from the lectures (if, for, operators, functions, built-in functions)...

  • 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

  • Program in C Student Data using Structs In this assignment you will create a program (using...

    Program in C Student Data using Structs In this assignment you will create a program (using multiple .c files) to solve the following problem: You have been hired by the university to create a program that will read in input from a .txt file. This input will contain a student name, student id #, their age and their current gpa. You should use the following struct for your student data: struct Student{                 char name[50];                 int id;                 float...

  • 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...

  • 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...

  • 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...

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