Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Count rows in Matlab

  1. Jun 13, 2008 #1
    I am am trying to determine the number of rows in a matrix using matlab,

    I usually use
    but this requires me to do the operation in 2 steps which will not work in my application.

    I need to be able to determine the number of rows in one step so I can use it as a parameter in a 'while' loop.

    I have tried using rows(X), but I get an error message. I haven't been able to find a solution and my code has come to a stand still. Surely there has to be a function or easy way to do this?

  2. jcsd
  3. Jun 13, 2008 #2
    Assuming X is rectangular use:

    rows = length(X(:,1));

    Although I find it surprising that you cannot use the code you had been using before, just execute it before the while loop starts.
  4. Jun 17, 2008 #3
    Thanks Crosson,
    here is part of the code i needed it for:

    while (3*num_nodes-DOF)<length(K_cols_s(1,:));
    if restx(i)==1;

    if restx(i)==0;

    As you can see, I was deleteing columns within the loop so i couldn't define the number of columns before hand as it was changing inside the loop. There might be a better way to do it but im very new to matlab, and I am just trying to learn as much as I can.

    One problem I am haveing tho is, my program requires alot of inputs from the command window, and it is easy to make a mistake when inputing the values when promted. Is there a easy way to rollback to the previous input if you make a mistake?

    The program is for solving 2d frames and trusses useing the matrix stiffness method in case you were wondering.

  5. Jun 17, 2008 #4
    The way to solve the input problem is to make a script file of Matlab commands.

    Here is a tutorial on how to do this, I think it would help your problem a lot:


    I tend to only use Matlab when I have to, I usually use Mathematica, so I can't give you a full tutorial but hopefully you can learn from the linked website like I did.
  6. Jun 17, 2008 #5
    I just skimmed, but if you want to return the size of a row in one step use r_size = size(X,1).

    for columns, use c_size = size(X,2)

    where X is a matrix
  7. Jun 19, 2008 #6
    Thanks FrogPad, I appreciate all the help with getting to know these operations in matlab.

    I hope you dont mind If I ask one more question tho (Il post here so I dont clog the forum up with my amature questions).

    I get an output like this from matlab after I run my program,

    Rs =

    Where Rs is the reaction forces,

    Is there any way I can get this to be displayed as,
    F1x = 0.0000
    F1y = -1.0000
    M1 = -1.0000

    I tried to make a vector called actions_Rs by doing the following,
    >> actions_RS={'F1x' 'F1y' 'M1'}'

    actions_RS =


    but when I try to put the 2 together I get
    > {actions_RS num2str(Rs)}

    ans =

    {3x1 cell} [3x11 char]

    This obviously isn't what I had in mind. I want to try and make it easy for other users to easily intemperate each reaction force.

    Thanks in advance

  8. Jun 19, 2008 #7
    So, I don't have MATLAB handy... so treat this as pseudo code.

    So Rs is a vector, in the case it happens to be a column vector (it has 3 rows, and 1 column).

    You can access each element of the vector like this:


    Or you can use a shorter notation and accomplish the same thing with:


    Now when you do this at the console, you will have a scalar variable (F1x,F1y,M1 are scalars).

    If you want to make string variables you can do something like this:

    str1 = strcat('F1x', num2str(Rs(1)))
    str2 = strcat('F1y', num2str(Rs(2)))
    str3 = strcat('M1', num2str(Rs(3)))

    I used the strcat function because I cna't remember the short hand for string concatination off hand.

    Now we can make the code really cryptic and do these in less lines, but I don't think that would help anything.

    Let me know if this helps, any more questions... just ask
  9. Jun 19, 2008 #8
    Thanks FrogPad, I will have a play around with those functions and see if I can get them to suit my application. The reason I tried to put all the names ie F1x F1y M1 in a vector is I was intending to use a loop to concatnate the 2 strings as the number of members I solve for changes, so I cant explicitly write a code for F1x F1y M1.

    If I refer to my initial attempt, I wanted to try the function with a for loop, something like

    for i=1:num_members
    supports_reactions(i)={actions_RS num2str(Rs)}

    I haven't had anything to do with strings yet so this might be quite a learning curve.

    Thanks for taking the time to help me out
    Regards Elbarto
  10. Jun 19, 2008 #9
    ahhh...... I just wrote up a very nice response to help you out, and I got logged out somehow and lost all of it!!!

    Let me very quickly try to repeat myself

    can you build a matrix "A" that is:
    [1 2 3]
    [4 5 6]

    Where F1x=1, F1y=2, M1=3
    F2x=4, F2y=5, M2=6

    you can then loop through this with:

    for i=1:size(A,2)
    B(i,1) = strcat('F', num2str(i), 'x=', num2str(A(i,1)))
    B(i,2) = strcat('F', num2str(i), 'y=', num2str(A(i,2)))
    B(i,3) = strcat('M', num2str(i), num2str(A(i,3)))
    Last edited: Jun 19, 2008
  11. Jun 19, 2008 #10
    Thanks alot FrogPad, that looks like it will work out well. I will let you know how I get on.

    Thanks Again
  12. Sep 20, 2011 #11
    i am new to matlab, i m writing a simple program,...
    i want to ask that, i have a matrix
    1 1 2 100
    2 2 3 200
    3 2 3 300
    4 3 4 400
    i want to save 4th column values for i=1:4 as k1=100,k2=200,k3=300,k4=400.
    how we will program in matlab this.

    thanks in advance
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook