Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
BCM
User Panel

Posted: 2/10/2014 12:10:23 AM EDT
Curious if anyone could help me out.  I am trying to write a function that takes a matrix as an input and returns a vector that contains the number of rows and columns in the matrix. The function should also print these values (number of rows, number of columns) to screen.

I hate matlab and I hate trying to write code in it, specially functions.  This is a homework assignment but I dont quite know how to go about this.  Why waste my time writing this function when I can just type "size(a)" a being a matrix that is already defined and it will give me a result like

ans
     R       C

R being the Rows and C being the Colums.

Anyone willing to help me out?  Im not asking for someone to do it for me maybe just give me some pointers on what to do...

This is where I am at but I dont think the professor will accept it since I am using the "size command"

function [ v ] = lab_2( matrix )
%Function that takes a Matrix input and outputs a vector.
%   Resultant Vector will contain the number of rows and colums in the matrix.


%Resultant Vector
v=size(matrix);

Link Posted: 2/10/2014 9:41:24 PM EDT
[#1]
That sounds like kind of a stupid assignment.  It's non-trivial to find size of a matrix in C, but Fortran (MATLAB's original basis) has traditionally had a built-in SIZE function, since in Fortran the size of a matrix is one of its intrinsic properties..

Here's a bullshit function that finds size of a (nonempty) matrix without using size() or length().  Note that it exploits the built-in intrinsic matrix properties anyway, but that's kind of the point .


function [r,c]=crappy_length(A)
%This function is a circuitous, inefficient, and tongue-in-cheek means of finding the size of a
%matrix "A"

%Copyright Feb 2014, MrFisher

i=1;

while 1==1
   try
       A(i,:);
   catch
       break
   end
   i=i+1;
end
r=i-1;
i=1;
while 1==1
   try
       A(:,i);
   catch
       break
   end
   i=i+1;
   
end
c=i-1;

Link Posted: 2/10/2014 10:14:15 PM EDT
[#2]
I taught an undergraduate MATLAB course for several years.

While a lot of students had trouble writing functions, it's important to remember that functions are one of the most important things you can learn in MATLAB.  They allow you to compartmentalize your code and break up large programs into simple elements that improve organization.  They let you easily redistribute that code to others so they can apply it to their problems.  They also allow you to use more advanced tools in MATLAB that require function handles as inputs.
Link Posted: 2/13/2014 5:33:25 PM EDT
[#3]
Thanks MrFisher!

Got a Chuckle out of the title of your function.

So I completely understand why we are learning to write function I just dont understand why we are trying to write a function for something that has a perfectly good command available for it.  I honestly just dont know where to begin for this function and as a student I feel like I would learn Matlab better if we were writing a function for something we deal with on a regular basis or something that may tie into the class we are doing.

I see why we are doing it just not the exact problem which has left me stumped.
Link Posted: 2/17/2014 4:51:30 PM EDT
[#4]
I'm not well versed in Matlab (my university did a terrible job teaching us even though we had a whole class on it ), but as I was reading the first part of your post, I was thinking "Why not just use "size"?"  
Link Posted: 2/18/2014 1:11:58 AM EDT
[#5]
Discussion ForumsJump to Quoted PostQuote History
Quoted:
I'm not well versed in Matlab (my university did a terrible job teaching us even though we had a whole class on it ), but as I was reading the first part of your post, I was thinking "Why not just use "size"?"  
View Quote


That's pretty well standard, unfortunately.  Our school has replaced the undergrad MATLAB course twice, and the curriculum still sucks.

Engineers end up hating it, which is unfortunate, because you can often use it to automate a huge amount of shit.
Link Posted: 3/5/2014 11:16:36 AM EDT
[#6]
It's insane how terrible it was.

The class' format was as follows:

1.  Professor hands out a paper with a problem statement (i.e. calculate some projectile motion or something).

2.  Professor spends two whole classes writing the code on the board, explaining bits of notation as he goes (never generically explaining what notation is required, just only bits and pieces).

3.  On the third class of the week, we'd have a lab where we sit at computers and type up and run code ourselves.  The problem was that the assignment was practically the same as the example in the class with a few numbers changed and maybe requiring an extra variable to be solved.

4.  Only two tests in the class, midterm and final.  Each test involved problem statements from the lab classes (again with slightly different numbers but same otherwise).  If you memorized your code line-for-line from the labs, you aced the test.

I got an A in the class, but I learned more Matlab from my Machine Design and Aerodynamics classes by an order of magnitude.  We've hired new engineers that have graduated five years after me, and apparently that class is still taught the same way.
Link Posted: 3/5/2014 6:03:49 PM EDT
[#7]
My university gave up on MATLAB courses. A controls professor I had (who loved to torture us with MATLAB ) once declared that any standard class format in MATLAB is worthless. The best way to learn is just by doing it, and learning it as you need it, I suppose.

Apparently, they had tried undergrad classes in MATLAB, and the minor improvement was not justification for a whole course.
Link Posted: 3/17/2014 3:18:29 AM EDT
[#8]
Discussion ForumsJump to Quoted PostQuote History
Quoted:
My university gave up on MATLAB courses. A controls professor I had (who loved to torture us with MATLAB ) once declared that any standard class format in MATLAB is worthless. The best way to learn is just by doing it, and learning it as you need it, I suppose.

Apparently, they had tried undergrad classes in MATLAB, and the minor improvement was not justification for a whole course.
View Quote


My university did a half semester 1 credit course where you have 2x 50 minute sessions per week.  I had previous MATLAB experience from community college courses, but I thought the university's way of doing it was at least decently helpful.

Outside of application specific areas, you really only need to understand the basics of scripts, functions, arrays, plots, and loops.  That shouldn't be too difficult to teach, though I'm sure that lots of universities mess it up somehow.
Link Posted: 6/11/2014 9:46:08 PM EDT
[#9]
Yea, my university first did away with Linear requirement for the ME/AE department and then combined linear with matlab and mathcad into one class. The result, we didn't learn a damn thing in that class.
Link Posted: 6/12/2014 8:14:44 AM EDT
[#10]
Op, I'm with you; that is a fucking stupid assignment. They should have had you write a function to invert a matrix, that would be easy enough and still teach you something.





My Matlab introduction was in 2000 with a Prof. that was fresh out of his post-doc. It took him 3 classes before he realized that we knew what the Start button was in Windows and could actually open a program without explicit directions... We also had some piss-poor 150 page soft-bound book that had heat transfer problems and other problems that were not well posed and completely out of scope for 2nd semester freshman students.





I actually learned more in my numerical methods class (Newton's Method, Riemann Sums, basic curve fitting) and my FEA class my senior year. However, my real learning experience was grad school; bessel functions, acoustic calculations, laplace transforms, cross-correlations and solving differential equations in fluid mechanics.





Grad school also taught me the beauty of LaTeX...
When you really start to understand Matlab you will write sub-functions and make your own data classes. I haven't figured out the data classes yet, but I have a couple co-workers that could give software engineers a run for their money.

 
Link Posted: 7/3/2014 3:33:37 AM EDT
[#11]
My matlab class was easily the most stressful class I've ever been in.  When it was all said and done my grade did not even remotely reflect my general sense of panic during the class!

Looking back I wish the professor had a little bit more of a structured curriculum...
Link Posted: 7/4/2014 3:52:17 PM EDT
[#12]
I used a version of matlab ported onto a cray.
Some of the tasks still took many many hours.

Think DSP with a vengeance.
When you are NOT the intended recipient of a signal it is often in very bad shape when you get a sample.
It is not like you can get on the channel and ask "Comrade, please speak slower and fix you transmitter."

Close Join Our Mail List to Stay Up To Date! Win a FREE Membership!

Sign up for the ARFCOM weekly newsletter and be entered to win a free ARFCOM membership. One new winner* is announced every week!

You will receive an email every Friday morning featuring the latest chatter from the hottest topics, breaking news surrounding legislation, as well as exclusive deals only available to ARFCOM email subscribers.


By signing up you agree to our User Agreement. *Must have a registered ARFCOM account to win.
Top Top