gpt4 book ai didi

matlab - 使用不同的起始/结束元素数 reshape 行

转载 作者:行者123 更新时间:2023-12-02 06:49:20 26 4
gpt4 key购买 nike

问题是:

Product of known dimensions, 3, not divisible into total number of elements, 16.

这是因为我想在3x6矩阵中 reshape 一个16x1矩阵。问题是起始矩阵有 16 个元素,最终矩阵有 18 个元素。是否有一种聪明的方法来按行 reshape 并用 0 填充缺失的元素,直到元素数量匹配?

当然,我需要一个独立于这些数字的通用方法,因为矩阵的大小可以改变。

TBN:0 应该位于矩阵末尾

最佳答案

方法#1

您可以使用vec2mat 这是通信系统工具箱的一部分,假设A作为输入向量 -

ncols = 6; %// number of columns needed in the output
out = vec2mat(A,ncols)

示例运行 -

>> A'
ans =
4 9 8 9 6 1 8 9 7 7 7 4 6 2 7 1
>> out
out =
4 9 8 9 6 1
8 9 7 7 7 4
6 2 7 1 0 0
<小时/>

方法#2

如果您没有该工具箱,您可以使用基本功能来实现相同的效果 -

out = zeros([ncols ceil(numel(A)/ncols)]);
out(1:numel(A)) = A;
out = out.'

关于matlab - 使用不同的起始/结束元素数 reshape 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com