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.

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

C++ Code :

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int i,j,r,c,n;
   r=2,c=4,n=4;
   int mat[2][4] = {5,2,3,1 ,7,6,3,8};      
   int vec[4]={1,9,8,3};
   printf("Matrix :\n");
   for(i=0;i<r;i++)
   {
  
       for(j=0;j<c;j++)
       printf("%d ",mat[i][j]);
       printf("\n");
   }
   printf("Vector :\n");
   for(i=0;i<n;i++)
   {
       printf("%d\n",vec[i]);
   }
  
   //since after multiplication final result will be
   //column vector of 2 elements
   int final[2][1];
   int sum=0;
   //calculating the matrix multiplication
   for(i =0;i<r;i++)
   {
       sum=0;
       for(j=0;j<c;j++)
       {
           sum = sum + mat[i][j]*vec[j];
       }
       final[i][0] = sum;
   }
  

  
   printf("Result after multiplication is :\n");
   for(i=0;i<r;i++)
   printf("%d\n",final[i][0]);
   return 0;
}

Matlab Code:

mat = [5 2 3 1;7 6 3 8];

vec = [1;9;8;3];

mat*vec

Output SS:

C++

Matlab:

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. works in matlab

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

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

  • Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp...

    Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp takes a single argument, which is the path to a file containing a square (k × k) matrix M and a non-negative exponent n. It computes M and prints the result Note that the size of the matrix is not known statically. You ust use malloc to allocate space for the matrix once you obtain its size from the input file. Tocompute M". it...

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

  • 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

  • In matlab, I keep getting the same issue for this problem can someone help? "myrowproduct.m" >> Filename...

    In matlab, I keep getting the same issue for this problem can someone help? "myrowproduct.m" >> Filename myrowproduct (A,x) Undefined function or variable 'Filename' Did you mean: mfilename : myrowproduct (A, x) Undefined function or variable 'myrowproduct' function y = myrowproduct(A,x); function y my rowp roduct (A, x); Error: Function definition not supported in this context. Create functions in code file. (, 2,,)T 4. The product y Ax of an m x n matrix A times a vector x= can...

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

  • MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds...

    MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds them (if compatible) and prints their sum, 2) subtracts them (if compatible) and prints their difference, and 3) multiplies them (if compatible) and prints their product. Prompt the user for the names of two matrix input files (see format below) and place the output in a file of the user’s choosing. The format of the input files should contain the number of rows, followed...

  • matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name...

    matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name the function [max_value, max_index] - LASTNAMES matrix max (x), where input argument x is a vector or matrix (not a scalar), and the outputs are the maximum value(s) and indexes of the maximum value(s) of the mput x. Do not use MATLAB functions max() or find(). Use loops and if branches to determine the maximum elements of matrix. Check for correct number and valid...

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