Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I need help with a simple MATLAB code

Status
Not open for further replies.

mmaah

Newbie level 4
Joined
Feb 16, 2009
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,351
Hello everybody,

I need a simple help with a small portion of MATLAB code.
I am trying to develop a program for simulating certain behavior in a machine. The entries are dependent. However at this point I wanna consider them as a 6X5 matrix. Therefore, the question is, how can I enter the elements row by row using MATLAB?

I managed to wrote a code to enter 2D array using for loops and input statement but I failed to adapt it to accept the entries in line format.

If you've any suggestion, I would be grateful.

Thank you
 

You can modify a matrix in matlab like so:

Code:
>> D(1,1:4) = [2 -5 4 -1]
D =
2 -5  4 -1  0
1 -2  1  0  0
0  1 -2  1  0
0  0  1 -2  1

So to modify or enter the second row:

Code:
>> D(2,1:5) = [0 0 0 0 0]
D =
2 -5  4 -1 0
0  0  0  0 0
0  1 -2  1 0
0  0  1 -2 1

Let me know if this is what your after. There are other ways to accomplish a single row entry into an existing matrix.

Added after 9 minutes:

I found this PDF on Matlab, thought it might help:

Working with matrices in MATLAB

There maybe other PDFs of interest on their site.
 

Dear "bigdogguru",

Thank you for your reply, at least you make me feel that there is a response to my question. However, my question was not about modifying or reading 2D array by assignment statements. What I want is exactly what you did but from "The Keyboard". I know how to enter a 2D array from the keyboard, but I faced a problem in entering it row by row..
Let me show you this code:

for i = 1:4
for j = 1:3
y(i,j) = input('Enter the elements row by row: ');
end
end

Try to execute this code and you will notice that the program will ask you enter your elements from the keyboard. Here, the elements to be entered row by row but each element will be given a new line in the command window. Therefore, the question is, how to modify this code so that it will accept the input in one line "from the command window" for each row.
Let me elaborate more:

let say I wanna enter this matrix A[1 2 3; 4 5 6; 7 8 9] from the keyboard:

>> 1 2 3
>> 4 5 6
>> 7 8 9
>> A
A =

1 2 3
4 5 6
7 8 9

Sorry for the long explanation, I hope it's clear now..

Thank you again.
 

Is this what you are after:

Code:
>> for i = 1:4 
   y(i,1:3) = input('Enter the elements row by row: '); 
   end
Enter the elements row by row: [1 2 3]
Enter the elements row by row: [2 3 4]
Enter the elements row by row: [3 4 5]
Enter the elements row by row: [4 5 6]
>> y

y =

     1     2     3
     2     3     4
     3     4     5
     4     5     6

Let me know.
 

    mmaah

    Points: 2
    Helpful Answer Positive Rating
Oh yeah! That's what I was looking for...
Thanks again for your help..

The best of luck
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top