Question

1. Given a matrix variable "mat", write code using for loops, if statements, etc. that will...

1. Given a matrix variable "mat", write code using for loops, if statements, etc. that will accomplish the same as the following:

matsum = sum(mat')

Please make sure to store the result in "matsum" and dont use sum.

This is what I have so far but it keeps telling me is wrong....

% mat has been initialized for you:

mat = randi([-100,100],randi([15,20]),randi([15,20]));

% write your statements that accomplish the code above:

[r c]=size(mat);

for i=1:c

matsum=0;

for j=1:r

matsum=matsum+mat(j,i);

end

fprintf('Sum of column %d:',i)

disp(matsum);

end

Homework Answers

Answer #1
  • The main point to remember here is that sum of a matrix is the sum of each of its columns. Sum of transpose should be the sum of each of its rows.
  • What was happening before is that with mat(j, i), the entries of each column were being added when it should be rows. Changing it to mat(i, j) works.
  • Also, first for-loop should be for rows and second for columns. That way, we stay at one row and add all its entries, which is what's needed here.

Please find the matlab code below for sum of transpose of a matrix.

% matrix initialization
mat = randi([-100,100],randi([15,20]),randi([15,20]));

% Expected output: disp(sum(mat'));
% Rows = 20, Columns = 18
[r c] = size(mat);
% Traversing through rows.
for i = 1:r
matsum = 0;
% Traversing through columns.
for j = 1:c
% Add the entries of each row.
matsum = matsum + mat(i, j);
end
fprintf('Sum of column %d:', i)
disp(matsum);
end

Know the answer?
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
Similar Questions
(5pts) Vectorize this code! Write one assignment statement that will accomplish exactly the same thing as...
(5pts) Vectorize this code! Write one assignment statement that will accomplish exactly the same thing as the give code (assume that the variable mat has been initialized). [r, c] = size(mat); for i = 1:r for j = 1:c mat(i,j) = 2*mat(i,j)^2 – 6*mat(i,j)+1; end end Use Matlab
write a java code. Write a program using loops to compute the sum of the 30...
write a java code. Write a program using loops to compute the sum of the 30 terms of the series below. 91/(3 + 2 + 2) + 92/(4 - 4 + 5) + 93/(5 + 6 - 8) + 94/(6 - 8 + 11) + 95/(7 + 10 + 14) + 96/(8 - 12 - 17) + . . . . Output: Term Ratio Sum 1 13 13 2 18.4 31.4 etc etc
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a non-negative integer value a. The script should then evaluates the sum Pn i=1 i a using a running sum for the case when n = 5. More specifically, use a counter for i that is used in the output fprintf as well as in the running sum. Feel free to write some code and just repeat it 5 times in your script. Sample...
MATLAB Write a script "readFile.m" which:   a. reads a given file "file.dat" into a matrix "XYZ",  ...
MATLAB Write a script "readFile.m" which:   a. reads a given file "file.dat" into a matrix "XYZ",   b. extracts the columns of the data from "XYZ" into arrays "c1", "c2", ..., "cN",   c. plots some column versus another column, with a title, and labeled axes.   To test and debug your code, create a "file.dat" with 3 rows and N=2 columns ( so a 3×2 matrix ).   The first column should be sorted. You can just enter some numbers in "file.dat" in...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...
Below is the problem that I have and here is the code that I have in...
Below is the problem that I have and here is the code that I have in c++. Can someone help me on what I am doing wrong or the correct code. A teacher has asked all her students to line up according to their first name. For example, in one class Amy will be at the front of the line, and Yolanda will be at the end. Write a program that prompts the user to enter the number of students...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...