Create multidimensional matrices from two multidimensional cells.

조회 수: 4 (최근 30일)
Sumedh Kulkarni
Sumedh Kulkarni 2018년 2월 8일
댓글: Matt 2018년 2월 12일
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.

채택된 답변

Matt
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
Stephen23
Stephen23 2018년 2월 9일
편집: Stephen23 2018년 2월 9일
"The matrices you are starting with have one dimension"
I thought that MATLAB does not have any concept of "one dimensional" matrices (unlike some languages). @Matt: Can you please show a reference to where these are defined in the MATLAB documentation?
Matt
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)
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by