Capture.PNG
Hi everyone,
The above 4x12 is a cell array. Each cell contains numbers. What I want to do next is matrix multiplication between all elements of row 2 with row 4. So, (1,2)*(4,2). This will give a 33by33 matrix. Next, I want to store it in a new variable. But since this is a cell structure I am not able to do it and need help.
How can I concatenate it? Or should i convert the cell array to something else to make my life easier?
Thank you for your help.

 채택된 답변

Kevin Phung
Kevin Phung 2019년 1월 23일

1 개 추천

use cell brackets to access contents of the cell array.
so if c is your 4x12 cell array, then c{2,1} would retun the array inside instead of the cell.

추가 답변 (1개)

Guillaume
Guillaume 2019년 1월 23일
편집: Guillaume 2019년 1월 23일

1 개 추천

result = cellfun(@times, yourcellarray(1, :), yourcellarray(4, :), 'UniformOutput', true)
will create a 1x12 cell array of 33x33 matrices. The same implemented as a loop:
result = cell(1, size(yourcellarray, 2));
for idx = 1:size(yourcellarray, 2)
result{idx} = yourcellarray{1, idx} .* yourcellarray{4, idx};
end
If you want a 33x33x12 matrix after that:
m = cat(3, result{:})
edit: got the wrong rows
edit again: and messed up the 'UniformOutput'

댓글 수: 4

Ishita Trivedi
Ishita Trivedi 2019년 1월 23일
Hello,
I don't fully understand @cellfun or how to use it in matlab. So if you dont mind, I would appreciate a little more explanation here. What does this line do exactly?
result = cellfun(@times, yourcellarray(1, :), yourcellarray(4, :), 'UniformOutput', false)
By "@times" does it do multiplication?
Guillaume
Guillaume 2019년 1월 23일
The cellfun will do exactly the same thing as the loop I've also given in my answer.
All cellfun does is iterate simultaneously over the elements of all the inputs (in this case, yourcellarray(1, :) and yourcellarray(4, :), pass these elements to the given the function (in the case, the times function which is indeed .*) and concatenate the outputs into a matrix or cell array (if 'UniformOutput' is true as it should have been in my answer, don't know what I wrote false).
If you're not comfortable with cellfun use the loop version which will give you the same result (I'm not sure why you accepted the other answer which didn't give you a proper solution).
Ishita Trivedi
Ishita Trivedi 2019년 1월 23일
I did end up using the loop but I would like to get more comfortable with the cell function.
As for the other answer, my for loop wasn't working because I wasn't using the curly braces - which makes sense since i needed to access the contents of the cell. I didn't explicity post what i had already tried but that was my error in the for loop. I guess it was luck but I thought Kevin figured i am having trouble because I am not accesing the contents of the cell correctly - which is true and is also clear in your answer. Thank you for you help.
Guillaume
Guillaume 2019년 1월 23일
No problem. Is cellfun clearer now?

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

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2019년 1월 23일

댓글:

2019년 1월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by