How do i assign value of a sub-cell element to a variable

function [g]= rock(s, m, h)
rock ({{[7,6,4,8,7,9,1,12], [9,6], 31}
{[9,3,5,8,0,1,1,1,15], [10,10]}
{[0,0,2,4,0,0,11], [3,4], 26}}, 55,45)
if i pass this i want to add -[7,6,4,8,7,9,1,12] = 54 and [9,6] = 15
i want to assign 7 to a variable and then run a loop uptil the end of the first element (i.e-12) so i can add them. How can i do that?
when i use cell2mat
b=cell2mat(s{1,1});
x=sum(b)
it adds up all the elements if the cell array (i.e [7,6,4,8,7,9,1,12], [9,6], 31) instead of first element

답변 (1개)

Ajay Kumar
Ajay Kumar 2019년 12월 5일
{sum([7,6,4,8,7,9,1,12]), sum([9,6]), 31}
Is this what you wanted?

댓글 수: 5

shubham shubham
shubham shubham 2019년 12월 5일
편집: shubham shubham 2019년 12월 5일
yes but i want to do it inside the function and not in the command window
function [g]= Untitled(s, m, h)
s = [sum(s{1,1}) sum(s{1,2}) sum(s{1,3})];
disp(s);
it's showing me this error
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in score (line 8)
x=sum(s{1,1});
what are you passing into s?
could you write it here.
b=cell2mat(s{1,1});
x=sum(b)

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

카테고리

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

질문:

2019년 12월 5일

댓글:

2019년 12월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by