Create multidimensional matrices from two multidimensional cells.
4 次查看(过去 30 天)
显示 更早的评论
Dear all, I have a cell f{k} with k matrices inside it, where k=1:96. For each k, the matrices have different lengths. For eg. length (f{1})=13 and length (f{96})=80. In addition, I have another cell W{k} with k=1:96. Again, for each k, the matrices have different lengths. For eg. length (W{1})=49 and length (W{96})=65.
Following is the output for cell f, for the first 9 elements of cell f:
Columns 1 through 9
[13×1 double] [24×1 double] [36×1 double] [43×1 double] [48×1 double] [65×1 double] [81×1 double] [95×1 double] [107×1 double]
Similarly, following is the output for cell W, representing first 9 elements of W:
Columns 1 through 9
[1×49 double] [1×49 double] [1×49 double] [1×48 double] [1×47 double] [1×49 double] [1×49 double] [1×48 double] [1×47 double]
Now, for each k=1:96, I want to create a matrix (say M) which should contain indices of W and f. For eg. if i put M(1), I should get matrix with size (f{1}*W{1}). Note that f{1} and W{1} have different lengths and new matrices will be formed of different lengths, for each k.
Is this even possible? Please help me with this. Thank you in advance.
0 个评论
采纳的回答
Matt
2018-2-8
Hi Sumedh,
The matrices you are starting with have one dimension. So to multiply each of the 96 matrices, you just have to be sure that the "inner dimensions" match, like (MxN)*(NxP) = (MxP), where in your case N = 1. You can iterate over the cell arrays to multiply the matrices, and assign the results to a new cell array. You can also measure the size (row number and column number) and assign that to a matrix. I wrote a sample code that does this. I hope this answers your question.
Matt
f = {rand(4,1),rand(3,1),rand(5,1)}
W = {rand(1,7),rand(1,2),rand(1,3)}
MAT = {};
dims = []
for k=1:numel(f)
MAT{k} = f{k}*W{k};
dims(k,:) = size(MAT{k});
end
3 个评论
Matt
2018-2-12
Hi Stephen,
To be specific about what I mean by "one dimensional", I mean that the matrices Sumedh is using either have one column or one row, so you only need one index to denote the position of an element. There still are two dimensions technically, it's just one is one element long.
I hope this helps.
Matt
更多回答(1 个)
Jos (10584)
2018-2-8
The operator * may not do what you expect. Try this
A = [1 2 3] ; B = [4 5 6] ; C = [10 20 30 40 50]
% A * B % error
A * B.' % dot-product
% A * C % error
A.' * B % → matrix
A.' * C % → matrix
% A * C.' % error
What type of operation are you after?
Or do you want to do element-wise multiplication (.*)? Then the two vectors should have an equal number of elements. Anyways, here is a one-liner that might suit you:
M = arrayfun(@(k) F{k} * W{k}, 1:numel(F), 'un', '0)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!