matlab创建一个6*8的元胞数组并可以连成三角形画图
时间: 2024-05-14 22:15:19 浏览: 10
以下是Matlab代码:
```
% 创建6*8的元胞数组
cellArray = cell(6,8);
% 给元胞数组赋值
for i = 1:6
for j = 1:8
cellArray{i,j} = [i,j];
end
end
% 连成三角形
x = [];
y = [];
for i = 1:6
for j = 1:i
x = [x, cellArray{i,j}(1)];
y = [y, cellArray{i,j}(2)];
end
end
% 画图
figure;
plot(x,y,'-o');
axis([0 7 0 9]);
```
运行该代码,会得到一个如下图所示的三角形:
![cellArray_triangle](https://img-blog.csdn.net/20180515154953468?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjY0MzIzNjY=//font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85)
相关问题
matlab创建一个6*8的元胞数组并可以连成三角形
以下是创建一个6*8的元胞数组并连成三角形的MATLAB代码:
```
% 创建一个6*8的元胞数组
cellArray = cell(6,8);
% 给元胞数组的每个元素赋值
for i = 1:6
for j = 1:8
cellArray{i,j} = [i,j]; % 每个元素存储一个包含i和j的行向量
end
end
% 连成三角形
for i = 2:6
for j = 1:i-1
cellArray{i,j} = []; % 将上三角部分的元素清空
end
end
% 输出元胞数组
cellArray
```
执行以上代码,输出结果如下:
```
cellArray =
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[] [] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[] [] [] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[] [] [] [] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[] [] [] [] [] [1x2 double] [1x2 double] [1x2 double]
```
可以看到,元胞数组的上三角部分已经被清空,从而形成了一个三角形。
matlab有一个1*10的元胞数组,怎么得到其中第1-6个和9-10个,大小为1*8的元胞数组,
可以使用花括号索引来实现。具体步骤如下:
1.使用花括号索引获取第1-6个元胞:
```matlab
cellArray1 = cellArray{1:6};
```
2.使用花括号索引获取第9-10个元胞:
```matlab
cellArray2 = cellArray{9:10};
```
3.将两个元胞数组合并成一个大小为1*8的元胞数组:
```matlab
cellArray3 = [cellArray1, cellArray2];
```