Count rows in Matlab

  • MATLAB
  • Thread starter elbarto
  • Start date
  • #1
33
0

Main Question or Discussion Point

Hi,
I am am trying to determine the number of rows in a matrix using matlab,

I usually use
>>[rows,cols]=size(X);
>>rows
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?

Thanks
Elbarto
 

Answers and Replies

  • #2
1,250
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.
 
  • #3
33
0
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;
K_cols_s(:,i)=K_cols_s(:,i);
end

if restx(i)==0;
K_cols_s(:,i)=[];
restx(i)=[];
i=i-1;
end
i=i+1;
end


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.

Thanks
Elbarto
 
  • #4
1,250
2
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:

http://web.cecs.pdx.edu/~gerry/MATLAB/programming/scripts.html#scriptsVSfuncs

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.
 
  • #5
809
0
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
 
  • #6
33
0
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 =

0.0000
-1.0000
-1.0000
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 =

'F1x'
'F1y'
'M1'

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

Elbarto
 
  • #7
809
0
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:


F1x=Rs(1,1)
F1y=Rs(2,1)
M1=Rs(3,1)

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

F1x=Rs(1)
F1y=Rs(2)
M1=Rs(3)

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
 
  • #8
33
0
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)}
end

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
 
  • #9
809
0
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)}
end

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
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)))
end
 
Last edited:
  • #10
33
0
Thanks alot FrogPad, that looks like it will work out well. I will let you know how I get on.

Thanks Again
Elbarto
 
  • #11
1
0
hello,
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
 

Related Threads for: Count rows in Matlab

  • Last Post
Replies
1
Views
6K
Replies
0
Views
7K
Replies
6
Views
7K
Replies
7
Views
21K
Replies
1
Views
8K
Replies
1
Views
5K
Top