MATLAB Answers

TABLE OF 2 ROWS AND 12 COLUMNS

21 views (last 30 days)
F.O
F.O on 10 Oct 2017
Commented: Walter Roberson on 11 Oct 2017
Hi,I want to make a TABLE OF 2 ROWS AND 12 COLUMNS WITH YEAR IN THE FIRST ROW AND NUMBER OF EARTHQUAKES IN THE SECOND ROW.
year=[2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 ];
n_of_earthquakes=[1 2 4 0 1 1 1 2 2 1 1 0]

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 11 Oct 2017
You can't have a table like that. You can however have them in columns:
year=[2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 ];
n_of_earthquakes=[1 2 4 0 1 1 1 2 2 1 1 0]
t = table(year', n_of_earthquakes', 'VariableNames', {'year', 'n_of_earthquakes'})
If you want a matrix (a regular double numerical array) rather than a "table" variable, you can just use seimcolon:
m = [year; n_of_earthquakes]

  1 Comment

F.O
F.O on 11 Oct 2017
Thanks ,this looks good.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 10 Oct 2017
year = 1973 : 1984;
earthquakes = randi(10000, 1, 12);
YourTable = array2table([year; earthquakes], 'RowNames', {'year', '# earthquakes'})

  6 Comments

Show 3 older comments
Walter Roberson
Walter Roberson on 11 Oct 2017
Not exactly. You can do
year = [2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 ];
ylab = arrayfun( @(Y) sprintf('yr%d', Y), year, 'uniform', 0);
n_of_earthquakes = [1 2 4 0 1 1 1 2 2 1 1 0];
YourTable = array2table( n_of_earthquakes, 'VariableNames', ylab, 'RowNames', {'# earthquakes'} )
YourTable =
1×12 table
yr2005 yr2006 yr2007 yr2008 yr2009 yr2010 yr2011 yr2012 yr2013 yr2014 yr2015 yr2016
______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______
# earthquakes 1 2 4 0 1 1 1 2 2 1 1 0
Notice the 'yr' in the column names. The exact text there is not important, but it is required that the names there are valid MATLAB variable names, so you cannot just use numbers. You could use 'CE' or 'Y' or 'AD' or the like, but the result must be a valid variable name.
F.O
F.O on 11 Oct 2017
Thanks a lot and this was what i wanted eventhough I didn't know what is this command
ylab = arrayfun( @(Y) sprintf('yr%d', Y), year, 'uniform', 0);
Walter Roberson
Walter Roberson on 11 Oct 2017
That command take the vector year, and for each element of it, applies the function @(Y) sprintf('yr%d', Y) . That function converts the numeric element to a string and puts the characters 'yr' in front of that, giving a character vector as a result for each numeric element. The 'uniform', 0 option tells MATLAB to put the results into a cell array, so the end result stored in ylab is a cell array, each element of which is a character vector that starts with 'yr' and then followed by the year.
Starting in R2017a, the entire line can be replaced with
ylab = "yr" + year;

Sign in to comment.

Sign in to answer this question.

Tags

Products