MATLAB 连接具有不等维数的矩阵

发布于 2024-11-13 08:29:57 字数 214 浏览 2 评论 0原文

有没有简单的方法可以使用零填充来连接具有不等维度的矩阵?

short = [1 2 3]';
long = [4 5 6 7]';
desiredResult = horzcat(short, long);

我想要这样的东西:

desiredResult = 
1 4 
2 5
3 6
0 7

Is there any easy way to concatenate matrices with unequal dimensions using zero padding?

short = [1 2 3]';
long = [4 5 6 7]';
desiredResult = horzcat(short, long);

I would like something like:

desiredResult = 
1 4 
2 5
3 6
0 7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪漫人生路 2024-11-20 08:29:57

当您分配给矩阵当前边界之外的索引时,MATLAB 中的矩阵会自动增长并用零填充。例如:

>> short = [1 2 3]';
>> long = [4 5 6 7]';
>> desiredResult(1:numel(short),1) = short;  %# Add short to column 1
>> desiredResult(1:numel(long),2) = long;    %# Add long to column 2
>> desiredResult

desiredResult =

     1     4
     2     5
     3     6
     0     7

Matrices in MATLAB are automatically grown and padded with zeroes when you assign to indices outside the current bounds of the matrix. For example:

>> short = [1 2 3]';
>> long = [4 5 6 7]';
>> desiredResult(1:numel(short),1) = short;  %# Add short to column 1
>> desiredResult(1:numel(long),2) = long;    %# Add long to column 2
>> desiredResult

desiredResult =

     1     4
     2     5
     3     6
     0     7
水染的天色ゝ 2024-11-20 08:29:57

编辑:

我已经编辑了之前的解决方案,这样您就不必向函数提供 maxLength 参数。该函数在进行填充之前计算它。

function out=joinUnevenVectors(varargin)
%#Horizontally catenate multiple column vectors by appending zeros 
%#at the ends of the shorter vectors
%#
%#SYNTAX: out = joinUnevenVectors(vec1, vec2, ... , vecN)

    maxLength=max(cellfun(@numel,varargin));
    out=cell2mat(cellfun(@(x)cat(1,x,zeros(maxLength-length(x),1)),varargin,'UniformOutput',false));

将其作为函数的便利之处在于,您可以轻松地将多个不均匀向量连接在一行中,例如 joinUnevenVectors(vec1,vec2,vec3,vec4) 等,而无需手动输入每行。

例子:

short = [1 2 3]';
long = [4 5 6 7]';
joinUnevenVectors(short,long)

ans =

     1     4
     2     5
     3     6
     0     7

EDIT:

I have edited my earlier solution so that you won't have to supply a maxLength parameter to the function. The function calculates it before doing the padding.

function out=joinUnevenVectors(varargin)
%#Horizontally catenate multiple column vectors by appending zeros 
%#at the ends of the shorter vectors
%#
%#SYNTAX: out = joinUnevenVectors(vec1, vec2, ... , vecN)

    maxLength=max(cellfun(@numel,varargin));
    out=cell2mat(cellfun(@(x)cat(1,x,zeros(maxLength-length(x),1)),varargin,'UniformOutput',false));

The convenience of having it as a function is that you can easily join multiple uneven vectors in a single line as joinUnevenVectors(vec1,vec2,vec3,vec4) and so on, without having to manually enter it in each line.

EXAMPLE:

short = [1 2 3]';
long = [4 5 6 7]';
joinUnevenVectors(short,long)

ans =

     1     4
     2     5
     3     6
     0     7
可可 2024-11-20 08:29:57

当写入矩阵中不存在的元素时,Matlab 会自动进行填充。
因此,另一种非常简单的方法如下:

短=[1;2;3];

长=[4;5;6;7];

短(1:长度(长),2)=长;

Matlab automatically does padding when writing to a non-existent element of a matrix.
Therefore, another very simple way of doing this is the following:

short=[1;2;3];

long=[4;5;6;7];

short(1:length(long),2)=long;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文