MATLAB如何将一个数组的值分别赋给另一个数组

2024-11-29 08:13:52
推荐回答(2个)
回答1:

对于一个矩阵A,可以连续赋值,如
>> A(1:3)=1:2:5
A =
1 3 5
对于cell数组,连续赋值可以用一下格式
例1:赋以连续数字
>> test=cell(1,3);
>> test(1,1:3)=num2cell(1:3:9)
test =
[1] [4] [7]
cell数组中每一元素都是一个cell结构的,所以可以连续索引cell单元,赋以连续的cell结构的数据。注意不能用内容索引,即大括号{ },如test{1:3},会报错
>> test{1:3}=num2cell(1:3:9)
The right hand side of this assignment has too few values to satisfy
the left hand side.

>> test{1:3}=1:3:9
The right hand side of this assignment has too few values to satisfy
the left hand side.
例2:
>> test(1:3)={1,'b','c'}
test =
[1] 'b' 'c'
例3:赋以相同值
>> test(1:3)={0}
test =
[0] [0] [0]

回答2:

这问的是哪一出啊??!一个赋值语句(B=A,,把数组A赋给B )不就行了吗,要改变数组结构就请去参考A(:),A(S)之类的函数用法吧