我有2个矩阵。
第一个是名字。
[\w\W]*?
第二个是数字。
Names={'a','b','c'};
然后我有了结构名,它是名称和数字的组合,它们等于一些具有相同行和列的随机矩阵。
在这种情况下,我有6个组合(2 * 1 * 3),看起来像=
a=[1 3]; b=[4]; c=[2 4 5];
我想将其返回到n维矩阵。在这种情况下,它必须是5维的;
a1.b4.c2=[7 8 9; 10 11 14];
a1.b4.c4=[2 4 5; 3 4 7];
a1.b4.c5=[3 2 11; 4 7 8];
a3.b4.c2=[1 1 1; 3 5 12];
a3.b4.c4=[2 7 9 ; 10 11 12];
a3.b4.c5=[4 2 7 ; 5 6 8];
我想编写一个通用代码,该代码可以帮助我针对不同数量的名称和数字进行此工作,但我做不到。希望你能帮我!谢谢。
答案 0 :(得分:0)
要获取每个字段的可能数字的每种组合,请使用[Numbers{1:3}] = ndgrid(a,b,c);
。
eval
如您所描述的那样,将带编号的结构放在工作区中会使以编程方式访问它们变得很混乱,因此应尽可能避免使用它;但是仍然可以使用evalPattern = strjoin(strcat(Names,'%d'), '.'); % 'a%d.b%d.c%d'
firstNumbers = cellfun(@(n) n(1), Numbers); % [1 4 2]
firstElement = eval(sprintf(evalPattern,firstNumbers)); % returns the value of a1.b4.c2
result = nan([size(firstElement) size(Numbers{1}]);
for ii = 1:numel(Numbers{1})
iiNumbers = cellfun(@(n) n(ii), Numbers);
result(:,:,ii) = eval(sprintf(evalPattern,iiNumbers));
end
来访问它们。
void Application_AcquireRequestState(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
// CheckSession() inlined
if (Context.Request.Url.LocalPath != "/UIComponents/User/Login.aspx")
{
if (context.Session["Name"] == null)
{
FormsAuthentication.RedirectToLoginPage();
}
}
}
答案 1 :(得分:0)
好吧,花费的时间比我预期的要长,但是在满足以下要求的情况下,以下代码应适用于任意数量的名称和数字:
regexp
或类似的方式进行调整。ax.bx.cx....
都可以由您的应用程序存储在某些上级结构中(以前)。ax.bx.cx...
的显示顺序,并且矩阵尺寸相等。因此,脚本很长,而且-恐怕-需要一些解释。请问。基本思想是,只要特定的“子代”仍然是结构,就可以遍历该结构。这样可以确保结构的任意“深度”,即名称和数字的数量。
我扩展了您的数据,所以您看到,它也适用于(a)附加名称,(b)附加数字和(c)不同的矩阵大小。当然,它也适用于您的原始数据。
此外,一开始不需要Names
或Numbers
,因为这些信息是从(必须是上级的)结构中自动提取的。
(注意:写在Octave中。我试图验证一下,Matlab中的所有功能也都可用。如果不是这种情况,请报告任何问题。然后我将重构代码。)
% Structs given.
a1.b4.c2.d3 = ones(4, 4);
a1.b4.c4.d3 = ones(4, 4) * 2;
a1.b4.c5.d3 = ones(4, 4) * 3;
a1.b6.c2.d3 = ones(4, 4) * 4;
a1.b6.c4.d3 = ones(4, 4) * 5;
a1.b6.c5.d3 = ones(4, 4) * 6;
a2.b4.c2.d3 = ones(4, 4) * 7;
a2.b4.c4.d3 = ones(4, 4) * 8;
a2.b4.c5.d3 = ones(4, 4) * 9;
a2.b6.c2.d3 = ones(4, 4) * 10;
a2.b6.c4.d3 = ones(4, 4) * 11;
a2.b6.c5.d3 = ones(4, 4) * 12;
% REQUIREMENT: Store your structs in some superordinated struct.
super.a1 = a1;
super.a2 = a2;
% Initialize combined struct for names and numbers.
NamesNumbers = struct();
% Initialize Names cell array.
Names = {};
% Extract names and numbers from superordinated struct.
totalNames = 0;
totalNumbers = 1;
current = super;
while (isstruct(current))
fields = fieldnames(current);
totalNames = totalNames + 1;
totalNumbers = totalNumbers * numel(fields);
for iField = 1:numel(fields)
field = fields{iField};
name = field(1);
Names{totalNames} = name;
number = field(2:end);
if (isfield(NamesNumbers, name) == false)
NamesNumbers.(name) = str2num(number);
else
NamesNumbers.(name) = [NamesNumbers.(name) str2num(number)];
end
end
current = current.(fields{1});
if (isstruct(current) == false)
[nRows, nCols] = size(current);
end
end
% Extract all values from superordinated struct.
level = struct2cell(super);
while (isstruct([level{:}]))
level = struct2cell([level{:}]);
end
values = vertcat(level{:});
% Determine indices.
maxIdx = cellfun(@(x) max(x), struct2cell(NamesNumbers));
idx = zeros([totalNumbers, totalNames]);
factorProd = 1;
for iName = 1:totalNames
numbers = NamesNumbers.(Names{iName});
n = numel(numbers);
factorProd = factorProd * n;
inner = totalNumbers / factorProd;
resh = totalNumbers * n / factorProd;
outer = factorProd / n;
column = repmat(reshape(repmat(numbers, inner, 1), resh, 1), outer, 1);
START = (iName - 1) * totalNumbers + 1;
STOP = iName * totalNumbers;
idx(START:STOP) = column;
end
% Initialize output.
output = zeros([nRows nCols maxIdx']);
% Fill output with values.
for iIdx = 1:size(idx, 1)
temp = num2cell(idx(iIdx, :));
START = (iIdx - 1) * nRows + 1;
STOP = iIdx * nRows;
output(:, :, temp{:}) = values(START:STOP, :);
end