结合数字矩阵和字符串数组 [英] Combining a matrix of numbers, with array of strings
本文介绍了结合数字矩阵和字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用MatLAB以更巧妙的方式计算下面显示的代码:
I am trying to compute the code seen below, in a more clever way, using MatLAB:
f=zeros(5,2) %Values irrelevant.
y = {'A1', 'B1';
f(1,1) f(1,2);
f(2,1) f(2,2);
f(3,1) f(3,2);
f(4,1) f(4,2);
f(5,1) f(5,2)};
我得到的是f
矩阵,在两个向量上方均带有A1
和B1
文本.
What I get out is the f
matrix with the text of A1
and B1
above the two vectors.
对于更复杂的用途,我想有一种更简单的编写方法,但是我尝试了括号,方括号和花括号的任何组合,我想都可以使用.
I suppose there is a simpler way to write this, for more complex purposing, but I've tried any combination of parenthesis, brackets and curly brackets, num2str
that I could think of.
有什么建议吗?
推荐答案
最简单的解决方案是使用 num2cell
并将结果与字符串连接:
The simplest solution is to use num2cell
and concatenate the result with the strings:
y = [{'A1', 'B1'}; num2cell(f)];
示例
>> f = reshape(1:10, 2, [])';
>> y = [{'A1', 'B1'}; num2cell(f)]
y =
'A1' 'B1'
[ 1] [ 2]
[ 3] [ 4]
[ 5] [ 6]
[ 7] [ 8]
[ 9] [10]
这篇关于结合数字矩阵和字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文