Question

Question 8 Data Structures - arrays, matrices and strings (a) Write a MATLAB script that will...

Question 8
Data Structures - arrays, matrices and strings
(a)
Write a MATLAB script that will copy all of the negative values in a matrix,M, to a new vector, N.[7 marks]


(b) Write a MATLAB script that reads in two strings from the user and comparesthem, printing’same’if they are the same or the indexes of the characterswhich differ if they are different. For example, if the user typed in ’car’and ’cat’, the script would print ’the words differ in characters: 3’. For thisquestion, you can assume the two strings entered are of the same length.[6 marks]

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

Answer a)
------
To get all -ve value in matrix M to a new vector N, the following code can be used
M = [1 2 -4 5;-6 9 -10 8] %make a matrix
N = M( M < 0) %copy -ve value from M to N


Answer b)
-----
s1 = input('Enter string 1: ', 's');
s2 = input('Enter string 2: ', 's');
if s1 == s2
   disp('same');
else
   idx = find(s1 ~= s2);
   disp(['the words differ in characters: ', num2str(idx)]);
end


---
output
---
Enter string 1: > cat
Enter string 2: > car
the words differ in characters: 3

Add a comment
Know the answer?
Add Answer to:
Question 8 Data Structures - arrays, matrices and strings (a) Write a MATLAB script that will...
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
  • IN MATLAB Write a script that will prompt the user for strings and read them in,...

    IN MATLAB Write a script that will prompt the user for strings and read them in, store them in a cell array (in a loop), and then print them out.

  • c++ Write a function has Match that takes 3 parameters: 2 arrays of strings and an...

    c++ Write a function has Match that takes 3 parameters: 2 arrays of strings and an int that represents the length of each array The function returns true if at any position, the two arrays have the same string, false if not. For example: {"a", "b", "cat", "d"} and {"1", "2", "cat", "4"} would match

  • matlab progam A commonly used matrix operation in linea alpebrais matrix matrix multiplication. Write a script...

    matlab progam A commonly used matrix operation in linea alpebrais matrix matrix multiplication. Write a script that reads a matrix M from the that this is different from the clement wise Squaring of the matrix ( M 2). To do this, follow these steps income che of them (M2) Note a. Read a 2x2 matrix from the user and store it in M. Make sure you let the user know what to input b. Print the dimensions of the matrix,...

  • MATLAB Question: Write a script for the problem pictured. Please send the script, not just the...

    MATLAB Question: Write a script for the problem pictured. Please send the script, not just the printout. Make sure your script allows you to input a value for n so when prompted, in the command window, user types n value. The n value should be the f_k if k ==1 .... Please also send print outs if you can. BELOW IS A SAMPLE ANSWER THAT DID NOT PUT THE INPUT SCRIPT NEEDED FOR THIS QUESTION TO BE CORRECT. YOU CAN...

  • I need help with these two Matlab question (1)Write a script that will 1. prompt the...

    I need help with these two Matlab question (1)Write a script that will 1. prompt the user for a maximum x value. 2. create an x vector from 1 to the user’s maximum in steps of 0.1 3. create a y vector which is sin(x) 4. plot the x and y vectors using blue *s, using appropriate x and y labels and a title (2)Write a fives function that will receive two arguments for the number of rows and columns...

  • Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the...

    Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the inner dimensions must be the same. Write this as a function with input arguments the matrices to be multiplied and output argument the resultant matrix, use "assert" statement(s) to check that the input arguments are valid and print an error message if they are not. Test with A=[1,2,3;4,5,6] and B=[7,8;9,10;11,12] showing results for: (a) A*B, (b) B*A, and (c) A*A.

  • Using Matlab or Octave, I do not know how to write the script for this problem,...

    Using Matlab or Octave, I do not know how to write the script for this problem, please help. Must match sample output 5. Consider a 3 x 3 matrix given by A = 01 012 013 az az 633 ( dai 632 033 The trace of A is defined as tr(A)=2au. Given another 3 x 3 matrix bu bı2 bia B- bi brz bra | bgi bz2 by it can be shown that trAB') = tr(AB). Write a script that...

  • This question is for MATLAB. Write a script that continuously asks the user for a new...

    This question is for MATLAB. Write a script that continuously asks the user for a new sentence, until the user types the word "STOP". The program records all of the sentences the user wrote (not including "STOP") in a matrix with as many rows as needed but with an N number of columns. The user is prompted to provide N before typing in the first sentence. The output of the program is the array with the sentences in it, in...

  • MATLAB homework assignment (HW6), but this time using a "vectorized" approach. Write a script that prompts...

    MATLAB homework assignment (HW6), but this time using a "vectorized" approach. Write a script that prompts the user for N integers (set N-6), one at a time, using a while loop. Error-check to ensure that each number entered is an integer, displaying an error message if it is not. If it is an integer, store it in an array variable nums. Continue prompting until N integers have been successfully entered. (You may copy and paste your code from the previous...

  • Use MATLAB to solve this problem. Thank you. Upload the assignment to CANVAS, it should include...

    Use MATLAB to solve this problem. Thank you. Upload the assignment to CANVAS, it should include two things: the PDF and the zipped folder. Late assignments are not accepted. For the questions below, do not create the arrays or matrices by hand. Instead, use built-in MATLAB functions and shorthand notations. Otherwise, no points will be given. 1) Create a function called my product that computes the multiplication of the numbers in any vector using a while loop. The function should...

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