Writing a Matlab function for the first time

4 次查看(过去 30 天)
This is th first time I'm writing a Matlab function.I have written several FORTRAN programs earlier but not Matlab.
Now, my program essentially does (or I want it to do) the follwoing:
1) The function takes 3 arguments which are vectors. The 3 vectors are Params,X,Y
2) The program (or function) first gets the size of vectors X and Y respectively and assigns to variables xrows and yrows respectively
3) Now: actually the vectors X and Y are concatenated vectors. That is there may be 2 or more vectors (X1,X2,Y1,Y2,..) which are concatenated into X and Y the arguments
4) The program wants to split the vectors X and Y into X1,X2,Y1,Y2,X3,....
5) For the time beinmg ,I have defined the total number of concatenated vectors as total_sets inside the function.
6)I have written a code to get hthese concatenated vectors X1,X2,Y1,Y2 as below.Can anyone help me to do the encessary corrections to the code? I shall be grateful.
function [ error ] = ConcatenatingAllDataSets( Params,X,Y)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
%Below we get the number of rows for the vector X
xrows = size(X,1)
%Below we get the number of rows for the vector Y
yrows=size(Y,1)
%Below we define the total number of sets that are to be concatenated
%So, if total sets =1, it means that we will have one set of epsilon's
%If total sets is equal to 2, we will have 2 sets of epsilons
%Similarly, if total sets is equal to
total_sets=2
start_value=0
k=total_sets
for j=1,total_sets
for i=start_value+1,1,(xrows/k)
epsilon(j,i)=X(i);
sigma(j,i)=Y(i)
end
k=k-1
end
%Above we have separated concatenated values - X and Y separated into
%epsilon and sigma
end

采纳的回答

Andrew Newell
Andrew Newell 2012-3-6
I think what you are trying to do is convert a row vector into a matrix with total_sets number of rows. You can do this with two commands:
function [epsilon,sigma] = concatenatingAllDataSets(total_sets,X,Y)
epsilon = reshape(X,total_sets,[]);
sigma = reshape(Y,total_sets,[]);
(edited to add function statement)
  6 个评论
Andrew Newell
Andrew Newell 2012-3-6
Assuming you already have your vectors X and Y, you should run
total_sets = 2;
[epsilon,sigma] = concatenatingAllDataSets(total_sets,X,Y)
Shalini
Shalini 2012-3-6
I do not have the values yet.
I want to call this function by typing it in the command line and supplying the arguments.I want to check whether it does what I wanted it to.
I guess it is possible as I saw the video here: http://www.mathworks.in/support/2009a/matlab/7.8/demos/WritingAMATLABProgram.html
How to supply these vectors through command line?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by