Question

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

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"!

% mat has been initialized for you:

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

% write your statements that accomplish the code above:

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

%matlab code

% matrix with randomn values between -100 and 100
mat = randi([-100,100],randi([15,20]),randi([15,20]));
% matsum stores the sum of every column
matsum = sum(mat)

[row, column] = size(mat);
for i=1:column
sum = 0;
for j=1:row
sum = sum + mat(j,i);
end
fprintf('Sum of column %d:',i);
disp(sum);
end

% end matlab code

%{
output:

matsum =
Columns 1 through 13:
-351 295 -103 183 -353 -335 -304 271 -475 -434 187 116 323
Columns 14 through 18:
151 45 -27 43 131

Sum of column 1:-351   
Sum of column 2: 295
Sum of column 3:-103
Sum of column 4: 183
Sum of column 5:-353
Sum of column 6:-335
Sum of column 7:-304
Sum of column 8: 271
Sum of column 9:-475
Sum of column 10:-434
Sum of column 11: 187
Sum of column 12: 116
Sum of column 13: 323
Sum of column 14: 151
Sum of column 15: 45
Sum of column 16:-27
Sum of column 17: 43
Sum of column 18: 131

%}

Add a comment
Know the answer?
Add Answer to:
Given a matrix variable "mat", write code using for loops, if statements, etc. that will accomplish...
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
  • Write a program code with method stringX(String str) in Java using loops to removes the ‘x’...

    Write a program code with method stringX(String str) in Java using loops to removes the ‘x’ s in a string. If a string starts and ends with ‘x’, keep that x. Use only loops and if statements. Please provide an explanation. Thanks!!! Examples: stringX(“abxxxcd”) returns “abcd” stringX(“xkittenx”) returns “xkittenx” stringX(“x”)” returns “x” stringX(“xcaptainxhelloxhix”) returns “xcaptainhellohix”

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • Write a Java program that calculates the sum of a variable sized matrix using a two...

    Write a Java program that calculates the sum of a variable sized matrix using a two dimensional array. (ArraySum.java) The user should provide the number of rows and columns Test multiple user inputs (3 times 2, 4 times 4, 6 times 2, etc) A sum should be created for each row, each column, and the total for the matrix Ex.: How big would you like your matrix? Rows - ? 3 Columns -? 2 Please enter your row 1? Column...

  • use java and write in text a. Rewrite the following code segment using if statements. Assume...

    use java and write in text a. Rewrite the following code segment using if statements. Assume that grade has been declared as of type char. char grade= 'B';; switch (grade) { case 'A': System.out.println("Excellent"); break case 'B': System.out.println("Good"); default: System.out.println("you can do better”); } - b. write Java code that inputs an integer and prints each of its digit followed by ** e.gif the integer is 1234 then it should print 1**2**3**4 e.g. if the integer is 85 then it...

  • please write the code in C++ AND BE VERY SURE OF IT SINCE IT IS AN...

    please write the code in C++ AND BE VERY SURE OF IT SINCE IT IS AN ASSIGNMENT WITH LOTS OF MARKS - DON'T COPY FROM THE INTERNET I- For each of the following, write a single statement that performs the specified task. Assume that long integer variables value1 and value2 have been declared and value1 has been initialized to 200000. a)- Declare the variable longPtr to be a pointer to an object of type long. b)- Assign the address of...

  • please write the code on matlab   using for loop Also practice using for loops to combine...

    please write the code on matlab   using for loop Also practice using for loops to combine two vectors with the same number of elements. do in-between iterations one before the last iteration will have w/index=(last element-1)) = 4x(index=(last_element-1)) - 2(2) last iteration windex=last element)= 4*x[index=last element) - z(1) you need to figure out how to the change in index for vector z within the for loop.

  • Write a line of code that will declare a file variable named data. Assuming that the...

    Write a line of code that will declare a file variable named data. Assuming that the variable data was correctly created in the previous problem, write a line of code that will associate the variable with a file named grades.txt that is being opened for reading. Assuming that grades.txt only contains integers, write a line of code that will use the variable data to read in the first integer and store in a variable named test1 that has already been...

  • 17. (2 points) C++ Using a while loop, write the code statements to sum 10 integers...

    17. (2 points) C++ Using a while loop, write the code statements to sum 10 integers entered by a user. Display the sum. Hint: Be sure to declare, initialize, and use appropriate variables to count the repetitions and accumulate the sum.

  • Study guide Intro to PYTHON questions: 25) Write the code that will generate this output using...

    Study guide Intro to PYTHON questions: 25) Write the code that will generate this output using only while loops and if statements (no string multiplication). ###AAAAAAAA### ###BBBBBBBB### ###CCCCCCCC### ###AAAAAAAA### ###BBBBBBBB### ###CCCCCCCC### 26) Write code that asks the user for a size and prints a triangle of stars of that size that increases by two at each level. You can only use one while loop and any number of variables and counters you want, but you are limited to these tools....

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

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