如何在Matlab中构造单元格以特定格式存储值? [英] How to structure a cell to store values in a specific format in Matlab?
问题描述
我有一个代码,用于寻找小于特定值的两个数组之间的最佳组合.该代码一次只使用数组B的每一行中的一个值.
I have a code that looks for the best combination between two arrays that are less than a specific value. The code only uses one value from each row of array B at a time.
B =
1 2 3
10 20 30
100 200 300
1000 2000 3000
我正在使用的代码是:
B=[1 2 3; 10 20 30 ; 100 200 300 ; 1000 2000 3000];
A=[100; 500; 300 ; 425];
SA = sum(A);
V={}; % number of rows for cell V = num of combinations -- column = 1
n = 1;
for k = 1:length(B)
for idx = nchoosek(1:numel(B), k)'
rows = mod(idx, length(B));
if ~isequal(rows, unique(rows)) %if rows not equal to unique(rows)
continue %combination possibility valid
end %Ignore the combination if there are two elements from the same row
B_subset = B(idx);
if (SA + sum(B_subset) <= 2000) %if sum of A + (combination) < 2000
V(n,1) = {B_subset(:)}; %iterate cell V with possible combinations
n = n + 1;
end
end
end
但是,我想以不同于此代码将结果存储在单元格中的方式来显示结果.
However, I would like to display results differently than how this code stores them in a cell.
而不是在单元格V
中显示结果,例如:
Instead of displaying results in cell V
such as :
[1]
[10]
[300]
[10;200]
[1000;30]
[1;10;300]
这是首选:(每行X列在单元格中都占据特定位置)
在这里,这意味着它们应以cell(1,1)={[B(1,x),B(2,y),B(3,z),B(4,w)]}
的形式排列.其中x y z w
是具有选定值的列.这样显示的输出是:
This is preferred : (each row X column takes a specific position in the cell)
Here, this means that they should be arranged as cell(1,1)={[B(1,x),B(2,y),B(3,z),B(4,w)]}
. Where x y z w
are the columns with chosen values. So that the displayed output is :
[1;0;0;0]
[0;10;0;0]
[0;0;300;0]
[0;10;200;0]
[0;30;0;1000]
[1;10;300;0]
在每个答案中,通过从矩阵B的第一行到第四行选择一个值来确定组合.每一行有3列,并且每行只能一次选择一个值.但是,例如,如果不能使用B(1,2),它将被替换为零.例如如果不能使用B
的第1行,则B(1,1:3)
将为单个0.结果将为[0; x; y; z].
In each answer, the combination is determined by choosing a value from the 1st to 4th row of matrix B. Each row has 3 columns, and only one value from each row can be chosen at once. However, if for example B(1,2) cannot be used, it will be replaced with a zero. e.g. if row 1 of B
cannot be used, then B(1,1:3)
will be a single 0. And the result will be [0;x;y;z].
- 因此,如果从第一行中选择
2
,并且从第二行中选择20
,而第三行 >和 4th 行不包括在内,它们应显示为0.因此答案将是[2; 20; 0; 0]. - 如果仅使用第4行(例如1000),则答案应为[0; 0; 0; 1000]
- So, if
2
is chosen from the 1st row, and20
is chosen from the 2nd row, while the 3rd and 4th rows are NOT included, they should show a 0. So the answer would be [2;20;0;0]. - If only the 4th row is used (such as 1000 for example), the answer should be [0;0;0;1000]
总而言之,我要实现以下目标:
In summary I want to implement the following :
- 每个单元格包含B的每一行中的
length(B)
个值(基于组合) - 未用于组合的每个值应为0并打印在单元格中
- Each cell contains
length(B)
values from every row of B (based on the combination) - Each value not used for the combination should be a 0 and printed in the cell
我目前正在尝试实现此功能,但是我的方法无法正常工作.如果您需要更多信息,请告诉我.
I am currently trying to implement this but my methods are not working .. If you require more info, please let me know.
修改
我已经尝试在dfb的答案中实现代码,但是遇到困难,请看一下答案,因为它包含解决方案的一半.
I have tried to implement the code in the dfb's answer below but having difficulties, please take a look at the answer as it contains half of the solution.
推荐答案
我的MATLAB超级生锈,但是这样的事情不是您需要的吗?
My MATLAB is super rusty, but doesn't something like this do what you need?
arr = zeros(1,len(B))
arr(idx) = B_subset(:)
V(n,1) = {arr}
这篇关于如何在Matlab中构造单元格以特定格式存储值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!