matlab to r,matlab to R import structure
問題
I have a structure in matlab for example
A =
in: [200x1 double]
cols: {28x1}
rows: [200x28 double]
How do I create a data frame in R which has A.cols as the columns and A.rows as the header
and A.in as the data
I am using R.Matlab package but I get stuck when I read the mat file in, how do I do this?
Thanks for your help!
回答1:
In MATLAB: convert your A to a dataset and use export() function to create a text file (in stead of .mat file). Note that your column and row names must be cells of strings but not numeric vectors.
As your A.in is a numeric matrix:
A.rows = [11,12,13,14,15]; %# numeric vector
A.cols = {'A','B','C','D','E'}; %# cell of strings
A.in = magic(5); %# numeric matrix
DS = mat2dataset(A.in,'VarNames',A.cols, 'ObsNames',cellstr(num2str(A.rows')));
export(DS,'file','A.txt')
Name for your file as you desire, e.g. 'A.txt'.
You may check the contents of your file, called "A.txt" by:
type A.txt
In R choose the folder with your 'A.txt' file and use function read.table():
A
rownames(A)
A$Observations
Explore your desired MATLAB variable in R:
head(A)
# A B C D E
# 11 17 24 1 8 15
# 12 23 5 7 14 16
# 13 4 6 13 20 22
# 14 10 12 19 21 3
# 15 11 18 25 2 9
Please note that in the example you provided, your data is in A.rows, and row names are in A.in.
來源:https://stackoverflow.com/questions/26284790/matlab-to-r-import-structure
總結
以上是生活随笔為你收集整理的matlab to r,matlab to R import structure的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos下安装 php,Centos
- 下一篇: php ajax mysql 出错,ph