How to Input and Display a Matrix in Matlab?

In summary, the conversation discusses creating a program in Matlab that allows a user to input a matrix and displays it. It also covers the use of different functions such as disp and fprintf, and provides tips on using proper syntax and formatting in Matlab.
  • #1
Raghav Gupta
1,011
76

Homework Statement


I have to make program that a user inputs a matrix and program displays it.

Homework Equations

The Attempt at a Solution


I know the logic as in c++ I am able to display that.
Here,
Matlab:
m=input('Enter rows of matrix'); % Why not double quotes here as in cout of C++?
n=input('Enter columns of matrix');
display('Enter the matrix elements');% To use what disp or display or fprintf here?
for i= 1:m-1
   for  j=1:n-1
        arr[i][j]=input(''); % Here error is coming of unbalanced parentheses or brackets.
   end
enddisplay('The matrix elements are:');
for(i=0;i<m;i++)
    for(j=0;j<n;j++)
disp(arr[i][j]);
    disp('\t');
    end
    fprintf('\n');
end
 
Last edited:
Physics news on Phys.org
  • #2
Hi there,

Raghav Gupta said:
% Why not double quotes here as in cout of C++?
Matlab recognizes single quotes for input of strings.

Raghav Gupta said:
% To use what disp or display or fprintf here?

fprintf is to useful when displaying strings for which you want to control the format of the data inside your string.
disp is simply used to display a string, such as the one you typed above. Note, however, that using disp does not allow you to format your string whatsoever, and should only be used when displaying simple strings like these.
NB: There's no such function display in Matlab. Use disp.

An example of using fprintf is the following:
>> fprintf('My age is %d\n', 20)
>> My age is 20

You can type help fprintf into the command window to get more information on this. In this case, from what I gather, disp will serve your purpose just fine.

Raghav Gupta said:
arr[i][j]=input(''); % Here error is coming of unbalanced parentheses or brackets.

Here I am assuming that you want to store the user's input into a matrix element.
Do note that to call an element of a matrix by its row and column, the proper way is by doing it this way:
>> mat(r,c) = input('Please input an element: ');
where mat is the name of your matrix (it is stored as a variable in your workspace) - you have to define it first above this line of code (outside your for loop)


Raghav Gupta said:
display('The matrix elements are:');
for(i=0;i<m;i++)
for(j=0;j<n;j++)
disp(arr[i][j]);
disp('\t');
end
fprintf('\n');
end

Matlab's syntax is different from C++. Use the same format as you did for the for loop above. Use the colon operator to specify the range for i and j.
 
  • #3
JeremyG said:
There's no such function display in Matlab
display is functioning as disp only.Means display is working.
After correcting the changes I am still not able to input the values in matrix. Only one value able to input.
 
  • #4
What does your code look like now?
 
  • #5
JeremyG said:
What does your code look like now?
Like this
Matlab:
m=input('Enter rows of matrix\n');
n=input('Enter columns of matrix\n');

for i= 1:m-1
   for  j=1:n-1
        arr(i,j)=input('Enter the element\n');
   end
enddisplay('The matrix elements are:');
for i= 1:m-1
    for j=1:n-1
      
        disp(arr(i,j));
    display('\t');
    end
    fprintf('\n');
end
 
  • #6
Raghav Gupta said:
for i= 1:m-1
for j=1:n-1

just a word of note, if you want a matrix of m rows and n columns, you should be iterating from i =1:m and j=1:n

If you just want to display the matrix after the user has finished creating the matrix, you can just use the disp function.
like this:
>> disp(arr)

Raghav Gupta said:
After correcting the changes I am still not able to input the values in matrix. Only one value able to input.

I'm still not quite sure what you mean here.
 
  • #7
Raghav Gupta said:
After correcting the changes I am still not able to input the values in matrix. Only one value able to input.
In your nested for loops you will be able to enter one matrix element at a time. Is that what you're asking about?
 
  • #8
Mark44 said:
In your nested for loops you will be able to enter one matrix element at a time. Is that what you're asking about?
I'm asking about that the message "Enter the elements" should appear once instead of it coming in loop but elements we can enter one by one.
It's a complete different scenario here from C++ of creating matrix. How one can learn MATLAB if he knows basics of c++?

Also what is difference between disp and display here?

My MATLAB code finally
Matlab:
m=input('Enter rows of matrix\n');
n=input('Enter columns of matrix\n');

for i= 1:m
   for  j=1:n
        arr(i,j)=input('Enter the element\n'); % This message is appearing in loop every time.
   end
enddisplay('The matrix elements are:');        
        disp(arr);% No void ,main() function here as in C++,  Wow! we can simply display matrix instead of going in for loop again as in C++.
 
  • #9
So you want the message "Enter the elements: " to appear once and after that allow the user to key in element by element (without any visual prompts?)

An easy solution would be to do what you did before, which is to display the message right before entering the for loop.
As for the input function, while it is possible to just type this:
>> arr(i,j) = input(' ');
It would not be visually pleasing but if that is the requirement for your assignment then go ahead.

Another friendly note:
The input function does not need a \n at the end of the string, unlike the fprintf function. This way, the user's input will be entered beside your prompt, and after he clicks enter, Matlab automatically goes to the next line.
 
  • Like
Likes Raghav Gupta
  • #10
One thing I not got was that what is the difference between disp and display in matlab. I tried both. But not able to spot the difference.
 
  • #11
Hi, there are 2 main differences between disp and display

1. disp does not print the variable name or ans

Code:
a = 1;
disp(a)

1

display(a)

a = 
     1

2. disp prints nothing for built-in types (numeric types, char, struct, and cell) when the value is empty.

Code:
>> a = {};
disp(a)
display(a)

a =

     {}

Subtle difference. Sorry for the confusion earlier
 
  • Like
Likes Raghav Gupta

FAQ: How to Input and Display a Matrix in Matlab?

1. What is a Matlab matrix input program?

A Matlab matrix input program is a program written in the Matlab programming language that allows users to input data and store it in a matrix format. This matrix can then be used for various mathematical operations and data analysis.

2. How do I input data into a Matlab matrix?

To input data into a Matlab matrix, you can use the built-in function "input" or "inputdlg". These functions allow you to enter data manually or through a dialog box. You can also import data from external sources such as Excel files or text files.

3. How do I create a matrix in Matlab?

To create a matrix in Matlab, you can use the square brackets "[ ]" to define the rows and columns of the matrix. For example, [1 2 3; 4 5 6] creates a 2x3 matrix with the values 1, 2, 3 in the first row and 4, 5, 6 in the second row.

4. Can I perform mathematical operations on a Matlab matrix?

Yes, Matlab has built-in functions for various mathematical operations on matrices, such as addition, subtraction, multiplication, and more. You can also use the ".*" and "./" operators for element-wise operations on matrices.

5. How can I access specific elements in a Matlab matrix?

To access specific elements in a Matlab matrix, you can use the row and column indices within parentheses after the matrix name. For example, if A is a matrix, A(2,3) will access the element in the second row and third column of the matrix. You can also use ranges, such as A(1:3,2) to access multiple elements at once.

Similar threads

Replies
7
Views
2K
Replies
14
Views
3K
Replies
2
Views
6K
Replies
9
Views
4K
Replies
21
Views
3K
Replies
1
Views
1K
Replies
1
Views
1K
Back
Top