[初学笔记] cell 转换为 matrix,double 和 string,num,char的方式
来源:互联网 发布:海龙设计软件 编辑:程序博客网 时间:2024/06/05 14:07
1 cell 转换到 matrix,从而对cell里面的 数值进行运算
用 cell2mat() 函数
(1)如果你的cell都是数值
>> C = {[1] [2 3 4]; [5; 9] [6 7 8; 10 11 12]};
M = cell2mat(C)
M =
1 2 3 4
5 6 7 8
9 10 11 12
5 6 7 8
9 10 11 12
(2)复杂一点的cell,只对部分的cell进行运算
假设你的cell像我这样,第一列是数值,第二列是问题,只计算第一列的数值
yvyq =
[1] 'How much do you ...'
[2] 'Are you interest...'
[3] 'What about your ...'
[4] 'score your healt...'
[5] 'Do you mind rais...'
[6] 'Rate your lookin...'
[4] 'Are you a nice p...'
[3] 'the job is, well...'
[2] 'Money? poor or not?'
[1] 'Do your family a...'
[2] 'Are you interest...'
[3] 'What about your ...'
[4] 'score your healt...'
[5] 'Do you mind rais...'
[6] 'Rate your lookin...'
[4] 'Are you a nice p...'
[3] 'the job is, well...'
[2] 'Money? poor or not?'
[1] 'Do your family a...'
那么首先你要进行提取,比如subrow,subcol之类的
>> subcol = yvyq(:,1)
subcol =
[1]
[2]
[3]
[4]
[5]
[6]
[4]
[3]
[2]
[1]
[2]
[3]
[4]
[5]
[6]
[4]
[3]
[2]
[1]
然后才是转到matrix
>> ymat = cell2mat(subrow)
ymat =
1
2
3
4
5
6
4
3
2
1
2
3
4
5
6
4
3
2
1
最后,这组数据就可以进行任意运算了
2 cell 转换到 double的方式
在跑程序的时候出现的一个报错 Conversion to cell from double is not possible
(1)论坛里面的方法是 在 要输入的 cell数据上把 ( ) 换成 { }
运行出现Conversion to cell from double is not possible.时哪里的错?
http://www.ilovematlab.cn/thread-99702-1-1.html
(出处: MATLAB中文论坛)
http://www.ilovematlab.cn/thread-99702-1-1.html
(出处: MATLAB中文论坛)
比如下面的例子
for i = 1:10
b = char(cellstr(yvyq (i,2)));
yvyq {i,1} = input (b);
while ((yvyq {i,1} < 1) || (yvyq {i,1} > 10))
fprintf('\n\n Error! Invalid input! Please enter number from 1 to 10.\r\n');
yvyq {i,1} = input(b);
end
i = i + 1;
end
这两句都要换
yvyq {i,1} = input (b);
while ((yvyq {i,1} < 1) || (yvyq {i,1} > 10))
最后的结果输出会是每个 yvyq(I,1)都有数值保留,方便后期用cell数组进行运算
(2)但是,如果从开始进行用变量代替这一输出
最开始的时候我采用的是这个方法,但是在循环里面,矩阵中的 yvyq会无法单独保留每一个的数值,也就是说他们从头到尾保留的只是字符串。。。。
for i = 1:10
a = yvyq (i,1);
b = char(cellstr(yvyq (i,2)));
a = input (b);
a = yvyq (i,1);
b = char(cellstr(yvyq (i,2)));
a = input (b);
while ((a < 1) || (a > 10))
fprintf('\n\n Error! Invalid input! Please enter number from 1 to 10.\r\n');
a = input(b);
end
ysum = ysum + a;
i = i + 1;
end
fprintf('\n\n Error! Invalid input! Please enter number from 1 to 10.\r\n');
a = input(b);
end
ysum = ysum + a;
i = i + 1;
end
而且a的数值会一直被更新和取代
3 cell转换到character方式
同上面循环里的代码
用char()即可
b = char(cellstr(dvdq (j,2)))
4 cell转换到number的方式
cell2num(a);
5 cell 转换到 str的方式
c = cellstr(S)
S = ['abc '; 'defg'; 'hi ']
S =
abc
defg
hi
abc
defg
hi
c = cellstr(S)
c =
'abc'
'defg'
'hi'
'abc'
'defg'
'hi'
阅读全文
0 0
- [初学笔记] cell 转换为 matrix,double 和 string,num,char的方式
- char[] 转换为 string 和 string 转换为 char[]
- matlab中double、char和cell的互转
- long、string和CString类型转换,CString转double、char *
- c++中int double 与char,string之间的转换
- string 和char * char[]之间的转换
- string char *和char[] 之间的转换
- string const char *和char *的转换
- string转换为char*
- string转换为char*
- char*转换为string^
- string转换为char*
- string转换为char *
- char*转换为string^
- char[] 转换为 string
- 闲来无事,玩玩基本之基本!c/c++之 string或者char*转换为double的 atof !
- char转换为string的方法
- char转换为double;int转换为char
- jks文件转换成ctr,key文件
- 【解析】全球物联网平台的发展现状
- 有了Xshell,连接远程主机不是难事!
- wordCount相关RDD的深度解析
- Git LFS 入门指南
- [初学笔记] cell 转换为 matrix,double 和 string,num,char的方式
- Windows环境使用CMake编译 opencv3.3.0 && opencv_contrib3.3.0
- 历程剖析:阿里云自研HTAP数据库的技术发展之路
- 深度 | NB-IoT很猛,但别忘了LoRa:物联网标准后浪推前浪 谁更“浪”
- Spring集成Hibernate5_04-Service层
- JS跳转页面的方式,以及使用JS锚点定位
- jQuery实现等比例缩放大图片让大图片自适应页面布局
- Java图片缩略图裁剪水印缩放旋转压缩转格式-Thumbnailator图像处理
- 后台管理权限设计