矩阵扩展

为了方便对图像边界的处理,通常要把图像矩阵进行扩展。

原图:


代码:

fig = figure('NumberTitle', 'off', 'name', 'Happy');  
hold on;
  
LineWidth = 2;  
FontSize = 12;  
MarkerSize = 3;  
  
for i = 1 : 9  
    plot([i i], [-1 -9], 'k', 'LineWidth', LineWidth);  
    plot([1 9], [-i -i], 'k', 'LineWidth', LineWidth);  
end  

% 上下
for j = 3 : 8
    text(j-0.5, -1.5, ['U'  num2str(j-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
    text(j-0.5, -8.5, ['D'  num2str(j-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
end

% 左右
for i = 3 : 8
    text(1.5, -i+0.5, ['L'  num2str(i-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
    text(8.5, -i+0.5, ['R'  num2str(i-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
end

% 四角
text(1.5, -1.5, 'UL', 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
text(8.5, -1.5, 'UR', 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
text(1.5, -8.5, 'DL', 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
text(8.5, -8.5, 'DR', 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
  
axis equal;  
axis off;  
扩展图:

代码:

fig = figure('NumberTitle', 'off', 'name', 'Happy');  
hold on;
  
LineWidth = 2;  
FontSize = 12;  
MarkerSize = 3;  
  
for i = 0 : 10  
    plot([i i], [0 -10], 'k', 'LineWidth', LineWidth);  
    plot([0 10], [-i -i], 'k', 'LineWidth', LineWidth);  
end  

% 上下
for j = 3 : 8
    text(j-0.5, -0.5, ['U'  num2str(j-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
    text(j-0.5, -1.5, ['U'  num2str(j-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
    text(j-0.5, -8.5, ['D'  num2str(j-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
    text(j-0.5, -9.5, ['D'  num2str(j-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
end

% 左右
for i = 3 : 8
    text(0.5, -i+0.5, ['L'  num2str(i-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
    text(1.5, -i+0.5, ['L'  num2str(i-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
    text(8.5, -i+0.5, ['R'  num2str(i-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
    text(9.5, -i+0.5, ['R'  num2str(i-1)], 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
end

% 四角
for i = 1 : 2
    for j = 1 : 2
        text(j-0.5, -i+0.5, 'UL', 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
        text(j+7.5, -i+0.5, 'UR', 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
        text(j-0.5, -i-7.5, 'DL', 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
        text(j+7.5, -i-7.5, 'DR', 'FontSize', FontSize, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
    end
end

% 框出原图
plot([1 9], [-1 -1], 'r', 'LineWidth', LineWidth+1);  
plot([9 9], [-1 -9], 'r', 'LineWidth', LineWidth+1);  
plot([9 1], [-9 -9], 'r', 'LineWidth', LineWidth+1);  
plot([1 1], [-9 -1], 'r', 'LineWidth', LineWidth+1);  

% 框出扩展方法
plot([0 10], [-2 -2], 'b', 'LineWidth', LineWidth+1);  
plot([0 10], [-8 -8], 'b', 'LineWidth', LineWidth+1);  
plot([2 2], [0 -10], 'b', 'LineWidth', LineWidth+1);  
plot([8 8], [0 -10], 'b', 'LineWidth', LineWidth+1);  
  
axis equal;  
axis off;  



### Python 中使用 NumPy 扩展矩阵的方法 在 Python 中,NumPy 是一个强大的库,用于处理多维数组和矩阵扩展矩阵可以通过多种方式实现,包括向矩阵添加行、列或整个子矩阵。以下是几种常见的扩展矩阵方法,并附有示例代码。 #### 1. 使用 `numpy.vstack` 垂直堆叠矩阵 通过 `numpy.vstack` 函数可以将两个矩阵按垂直方向(行方向)堆叠在一起。这种方法适用于需要增加矩阵行数的情况。 ```python import numpy as np # 原始矩阵 matrix_a = np.array([[1, 2], [3, 4]]) # 要添加的行 row_to_add = np.array([[5, 6]]) # 垂直堆叠 result = np.vstack((matrix_a, row_to_add)) print(result) ``` 输出结果为: ``` [[1 2] [3 4] [5 6]] ``` 此方法适用于二维矩阵扩展[^1]。 #### 2. 使用 `numpy.hstack` 水平堆叠矩阵 如果需要扩展矩阵的列数,可以使用 `numpy.hstack` 函数。它将两个矩阵按水平方向(列方向)堆叠在一起。 ```python import numpy as np # 原始矩阵 matrix_a = np.array([[1, 2], [3, 4]]) # 要添加的列 column_to_add = np.array([[5], [6]]) # 水平堆叠 result = np.hstack((matrix_a, column_to_add)) print(result) ``` 输出结果为: ``` [[1 2 5] [3 4 6]] ``` 此方法适用于增加矩阵列数的操作[^1]。 #### 3. 使用 `numpy.concatenate` 综合扩展矩阵 `numpy.concatenate` 是一种更通用的矩阵扩展方法,可以根据指定的轴(axis)来合并多个矩阵。当 `axis=0` 时,相当于 `vstack`;当 `axis=1` 时,相当于 `hstack`。 ```python import numpy as np # 原始矩阵 matrix_a = np.array([[1, 2], [3, 4]]) # 要添加的矩阵 matrix_b = np.array([[5, 6]]) # 按行方向扩展 result_row = np.concatenate((matrix_a, matrix_b), axis=0) # 按列方向扩展 result_col = np.concatenate((matrix_a, matrix_b.T), axis=1) print("按行扩展的结果:\n", result_row) print("按列扩展的结果:\n", result_col) ``` 输出结果为: ``` 按行扩展的结果: [[1 2] [3 4] [5 6]] 按列扩展的结果: [[1 2 5] [3 4 6]] ``` 此方法提供了更大的灵活性,适用于复杂矩阵扩展场景[^1]。 #### 4. 使用 `numpy.insert` 插入行或列 如果需要在特定位置插入行或列,可以使用 `numpy.insert` 函数。此函数允许指定插入的位置和方向。 ```python import numpy as np # 原始矩阵 matrix_a = np.array([[1, 2], [3, 4]]) # 在第1行后插入新行 [5, 6] row_to_insert = np.array([5, 6]) result_row = np.insert(matrix_a, 1, row_to_insert, axis=0) # 在第1列后插入新列 [5, 6].T column_to_insert = np.array([5, 6]).reshape(2, 1) result_col = np.insert(matrix_a, 1, column_to_insert, axis=1) print("插入行的结果:\n", result_row) print("插入列的结果:\n", result_col) ``` 输出结果为: ``` 插入行的结果: [[1 2] [5 6] [3 4]] 插入列的结果: [[1 5 2] [3 6 4]] ``` 此方法适合需要精确控制插入位置的场景[^1]。 ### 注意事项 - 确保扩展矩阵或向量维度与原始矩阵匹配,否则会引发错误。 - 如果涉及随机生成矩阵或正态分布数据,可以结合 `numpy.random` 模块生成所需矩阵[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值