# Homework Help: Matlab matrix input program

1. Nov 21, 2015

### Raghav Gupta

1. The problem statement, all variables and given/known data
I have to make program that a user inputs a matrix and program displays it.

2. Relevant equations

3. The attempt at a solution
I know the logic as in c++ I am able to display that.
Here,
Code (Matlab M):

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
end

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

Last edited: Nov 21, 2015
2. Nov 21, 2015

### JeremyG

Hi there,

Matlab recognizes single quotes for input of strings.

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.

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)

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. Nov 21, 2015

### Raghav Gupta

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. Nov 21, 2015

### JeremyG

What does your code look like now?

5. Nov 21, 2015

### Raghav Gupta

Like this
Code (Matlab M):

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
end

display('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. Nov 21, 2015

### JeremyG

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)

I'm still not quite sure what you mean here.

7. Nov 21, 2015

### Staff: Mentor

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. Nov 21, 2015

### Raghav Gupta

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
Code (Matlab M):

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
end

display('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. Nov 21, 2015

### JeremyG

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.

10. Nov 22, 2015

### Raghav Gupta

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. Nov 22, 2015

### JeremyG

Hi, there are 2 main differences between disp and display

1. disp does not print the variable name or ans

Code (Text):
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 (Text):
>> a = {};
disp(a)
display(a)

a =

{}
Subtle difference. Sorry for the confusion earlier