how to apply loop on 2 cell arrays where operation required is column wise multiplication of vectors in each cell?

조회 수: 1 (최근 30일)
I have A=cell with 64 arrays each of 1X7 vector, and B= cell with 1806 arrays each of 7x3 matrices. I want to multiply each column of 7x3 matrix of cell B with transpose of A vectors i-e each column is multlipled element wise with 7x1 vector of cell A.for example
A{1}=[2 4 5 3 2 1 1]
B{1}=[1 0 0; 0 0 1;1 0 0;0 0 1;0 1 0;1 0 0;0 1 0]
R{1,1}=
[2 0 0
0 0 4
5 0 0
0 0 3
0 2 0
1 0 0
0 1 0]
and run the loop over all A and B such that the resultant is R{64,1806} cell
I tried this:
for m=1:numel(B)
for i=1:numel(A)
for k=1:3 %Bhas 3 columns
R=B{m}(:,k).*total_timereq_for_all_machines{i}';
end
count=count+1
T{m,i}=R;
end
end
but this is giving me R as 7X1 vector while I want R as 7X3 matrix. how can I make corrections in this?

채택된 답변

Star Strider
Star Strider 2017년 1월 19일
I can’t run your code because I don’t have your data.
See if this works:
for m=1:numel(B)
for i=1:numel(A)
T{m,i} = bsxfun(@times, A{i}, B{m}')';
count=count+1
end
end
Note that numel may not be your best option. (I’m not certain what it would return in your code.) Consider using size instead.
  댓글 수: 4
summyia qamar
summyia qamar 2017년 1월 19일
yes thankyou.. but I thought bsxfun doesnt work on cell arrays..I tried this too but may be I got logical error
Star Strider
Star Strider 2017년 1월 20일
My pleasure.
I tested it on the array you provided and it worked. You have to use the curly brackets ‘{}’ to do the necessary array indexing.
If you have problems with my code, please attach a ‘.mat’ file with some or all of your ‘A’ and ‘B’ data so I can test my code with it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by