矩阵创建Octave/Matlab,无环解决方案要求 [英] Matrix creation Octave / Matlab, loopless solution request

查看:105
本文介绍了矩阵创建Octave/Matlab,无环解决方案要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个像这样的矩阵

I want to create a matrix like

A = [0 0 0 0 1;
     0 0 0 1 1;
     0 0 0 1 1;
     0 0 0 1 1;
     0 0 1 1 1;
     0 1 1 1 1]

基于一个矢量,该矢量指示每行的"1"前面应有多少个"0":

based on a vector indicating how many '0's should precede '1's on each row:

B = [4 3 3 3 2 1]

是否有一种无环的方法来做到这一点?

Is there a loopless way to do this ?

推荐答案

您没有在问题中提到应该如何定义数组的水平大小(个数).

You don't mention in your question how the horizontal size of the array should be defined (the number of ones).

对于预定义的宽度,您可以使用以下代码:

For predefined width you can use this code:

width = 5;

A = cell2mat(arrayfun(@(x) [ zeros(1,x), ones(1,width-x) ], B, 'UniformOutput', false)');

如果您希望A的宽度最小,但每行至少仍为1:

If you want that A has minimal width, but still at least one 1 in every row:

A = cell2mat(arrayfun(@(x) [ zeros(1,x), ones(1,max(B)+1-x) ], B, 'UniformOutput', false)');

这篇关于矩阵创建Octave/Matlab,无环解决方案要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆