Count Rows in Matlab | Elbarto

In summary: X) K_cols_s(i,1)=K_cols_s(i,1); K_cols_s(i,2)=K_cols_s(i,2); K_cols_s(i,3)=K_cols_s(i,3); endwhere K_cols_s is the column vector of row numbers.However, when I run the code, I get the following error message:'
  • #1
elbarto
33
0
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
 
Physics news on Phys.org
  • #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
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 I am 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 a lot 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
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
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
Thanks FrogPad, I appreciate all the help with getting to know these operations in matlab.

I hope you don't mind If I ask one more question tho (Il post here so I don't 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
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
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 can't 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
elbarto said:
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 can't 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
Thanks a lot FrogPad, that looks like it will work out well. I will let you know how I get on.

Thanks Again
Elbarto
 
  • #11
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
 

1. How do I count rows in Matlab using "Elbarto"?

To count rows in Matlab using "Elbarto", you can use the size() function. This function returns the number of rows and columns in a given matrix or array. You can also use the length() function to count the number of rows in a vector.

2. Can I count rows in a table using "Elbarto" in Matlab?

Yes, you can count rows in a table using "Elbarto" in Matlab. You can use the height() function to count the number of rows in a table. This function returns the number of rows in a table as an integer value.

3. How can I count rows in a multidimensional array using "Elbarto" in Matlab?

To count rows in a multidimensional array using "Elbarto" in Matlab, you can use the size() function with the appropriate dimension specified. For example, to count the number of rows in the third dimension of a 3D array, you can use size(array, 3).

4. Is there a faster way to count rows in Matlab using "Elbarto"?

Yes, there are faster ways to count rows in Matlab using "Elbarto". Instead of using the size() function, you can use the numel() function, which returns the total number of elements in a matrix or array. You can then divide this number by the number of columns to get the number of rows.

5. Can I count rows in a text file using "Elbarto" in Matlab?

Yes, you can count rows in a text file using "Elbarto" in Matlab. You can use the textscan() function to read the text file and then use the size() function to count the number of rows in the resulting cell array. Alternatively, you can use the fgetl() function to read each line of the text file and count the number of lines using a loop.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
866
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Linear and Abstract Algebra
Replies
8
Views
890
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
Back
Top