产生单元阵列

发布于 2025-01-03 12:28:48 字数 563 浏览 1 评论 0原文

我想在 matlab 中生成如下元胞数组:

P=  {100;010;000;000;001}  
    {100;000;010;000;001} 
    {100;000;000;010;001} 
    {000;100;010;000;001} 
    {000;100;000;010;001} 
    {000;000;100;010;001}

其中 P= {5x3 cell} {5x3 cell} {5x3 cell} {5x3 cell} {5x3 cell} {5x3 cell};

来自 5x3矩阵中,每一列中的模式应该只有一个“1”,并且每个单元格中最后一行的第三列是“1”。

即: P{1}= 100;010;000;000;001

我应该怎么做?

从我到现在为止所做的:

PP=zeros(5,3);
P={1,6};
P={PP,PP,PP,PP,PP,PP};

我怎样才能将它们放入单元格中?

太感谢了

I want to produce a cell array as below in matlab:

P=  {100;010;000;000;001}  
    {100;000;010;000;001} 
    {100;000;000;010;001} 
    {000;100;010;000;001} 
    {000;100;000;010;001} 
    {000;000;100;010;001}

where P= {5x3 cell} {5x3 cell} {5x3 cell} {5x3 cell} {5x3 cell} {5x3 cell};

from the 5x3 matrix, the pattern should be only one '1' in each column and the third column is '1' at final row in every cell.

i.e : P{1}= 100;010;000;000;001

how should I do that?

From what I had made up till now:

PP=zeros(5,3);
P={1,6};
P={PP,PP,PP,PP,PP,PP};

how can I put the ones in the cell?

thank you so much

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

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

发布评论

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

评论(1

彼岸花似海 2025-01-10 12:28:48

试试这个:

x = unique(perms([0 0 2 4]),'rows'); %# we'll convert 2 to 10 and 4 to 100 later
xi = randperm(size(x,1)); %# permute rows randomly
n = 6;
xi = xi(1:n); %# k random rows
x = x(xi,:);
x = [ x ones(n,1) ]'; %# add 1s and transpose
%# convert to strings representing binary numbers, then to cell array
out = reshape(cellstr(dec2bin(x)), size(x,1), n);
%# split the cell array by columns
out = mat2cell(out, size(out,1), ones(1,n));

如果您实际上需要数字(0,10,100),而不是字符串,则代码会短一些:

x = unique(perms([0 0 10 100]),'rows');
xi = randperm(size(x,1));
n = 6;
xi = xi(1:n);
x = x(xi,:);
x = [x ones(n,1)]';
out = mat2cell(x,size(x,1),ones(1,n));

更新

根据您的评论,这里是新代码:

n = 6;
P = repmat({zeros(5,3)},1,n); %# output array preallocation

x = unique(perms([0 0 1 2]),'rows');
xi = randperm(size(x,1));
xi = xi(1:n);
x = x(xi,:);

c = [x ones(n,1)+2]'; %# column index
r = repmat((1:5)',1,n); %# row index
for k=1:n
    P{k}(sub2ind( [5 3], r(c(:,k)>0,k), c(c(:,k)>0,k) )) = 1;
end

如果您需要逻辑矩阵数组只需将第二行中的 zeros 替换为 false 即可:

P = repmat({false(5,3)},1,n);

Try this:

x = unique(perms([0 0 2 4]),'rows'); %# we'll convert 2 to 10 and 4 to 100 later
xi = randperm(size(x,1)); %# permute rows randomly
n = 6;
xi = xi(1:n); %# k random rows
x = x(xi,:);
x = [ x ones(n,1) ]'; %# add 1s and transpose
%# convert to strings representing binary numbers, then to cell array
out = reshape(cellstr(dec2bin(x)), size(x,1), n);
%# split the cell array by columns
out = mat2cell(out, size(out,1), ones(1,n));

If you actually need numbers (0, 10, 100), not strings, the code will be a little shorter:

x = unique(perms([0 0 10 100]),'rows');
xi = randperm(size(x,1));
n = 6;
xi = xi(1:n);
x = x(xi,:);
x = [x ones(n,1)]';
out = mat2cell(x,size(x,1),ones(1,n));

UPDATE:

According your comment, here is the new code:

n = 6;
P = repmat({zeros(5,3)},1,n); %# output array preallocation

x = unique(perms([0 0 1 2]),'rows');
xi = randperm(size(x,1));
xi = xi(1:n);
x = x(xi,:);

c = [x ones(n,1)+2]'; %# column index
r = repmat((1:5)',1,n); %# row index
for k=1:n
    P{k}(sub2ind( [5 3], r(c(:,k)>0,k), c(c(:,k)>0,k) )) = 1;
end

If you need the array of logical matrices just substitute zeros with false in the 2nd line:

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