Question

In: Computer Science

Using Matlab Write a program that print the following matrix. (hint: use nested for-loop and if-statement)...

Using Matlab

Write a program that print the following matrix. (hint: use nested for-loop and if-statement)

mat =

[2 -1 0 0 0

-1 2 -1 0 0

0 0 -1 2 -1

0 0 0 -1 2];

Solutions

Expert Solution

H = zeros(4,5);
for r = 1:4
    for c = 1:5
         if((r == 1 && c == 1) || (r == 2 && c == 2) || (r == 3 && c == 4) || (r == 4 && c == 5))
            H(r,c) = 2
         elseif((r == 2 && c == 1) || (r == 1 && c == 2) || (r == 2 && c == 3) || (r == 3 && c == 3) || (r == 4 && c == 4) || (r == 3 && c == 5))
            H(r,c) = -1
         end
    end
end
disp(H)

If you have any doubts please comment and please don't dislike


Related Solutions

Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
must use python Write a nested for loop that displays the following output using the same...
must use python Write a nested for loop that displays the following output using the same integer from part a:                                                     1                                                 1   2   1                                             1   2   4   2   1                                         1   2   4   8   4   2   1                                     1   2   4   8 16   8   4   2   1                                 1   2   4   8 16 32 16   8   4   2   1                             1   2   4   8 16 32 64 32 16   8   4   2   1                         1   2   4  ...
Write a complete program to sum the following series: (hint: use the for loop) 1 /2...
Write a complete program to sum the following series: (hint: use the for loop) 1 /2 + 1/ 4 + 1 /6 + ⋯ + 1 /100 PS: use C++ language please
Your objective is to write a well-documented simple program using classes, a loop, and nested ifs...
Your objective is to write a well-documented simple program using classes, a loop, and nested ifs to simulate an ATM using JAVA. 1. Create an ATM class with class variables name, pin, and balance, a constructor with parameters to assign values to the three instance variables, methods to get the name, pin, and balance, and methods to handle validated deposits and withdrawals ( deposit and withdrawal amounts cannot be negative, and withdrawal amount must not be greater than the existing...
Using Eclipse (Pyramid) Print out the following pyramid using nested loop. 1 12 123 1234 12345...
Using Eclipse (Pyramid) Print out the following pyramid using nested loop. 1 12 123 1234 12345 1) the source code (.java file), and 2) the screenshot of running results of each question.
Write a java program: Write a nested loop program creating an array list of three positions.  The...
Write a java program: Write a nested loop program creating an array list of three positions.  The first loop terminates with a sentinel value of which user is prompted for input to continue of quit.   Inside loop to enter in each position a prompted input of person's first name.  After the inside loop, edit the second array location making its value "Boston". Print the array's contents.
Write a program that uses a for loop to print One of the months of the...
Write a program that uses a for loop to print One of the months of the year is January One of the months of the year is February ...
Write a program that uses nested for loops to print a multiplication table. Your program should...
Write a program that uses nested for loops to print a multiplication table. Your program should print out the multiplication table starting a 1 and going to 12. Output should be like: 1 x 1 = .1 x 2 = 2
Write a Java program using jGRASP directions are as follows: Uses a while loop to print...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print out "n" lines as follows: 0 0 1 0 1 2 0 1 2 3 … 0 1 2 3 … n-1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT