Loop and write into a cell

Hello, i wanna accumulate the data in my script with a loop
l=[1,2,3,4,5,6,7,8,9,10]
S=[42600 35000 91850 20000 41060 30000 63100 10000 38500 30000]';
P=[65 840 50 176 48 638 14 130 32 45]';
X=S .* P;
sort(X,'descend');
a=sum(X);
p=cell(10,1);
c=cell(10,1);
for k=1:10
p{k}= (X(k)/a)*100
end
for k=1:10
c{k}=sum(p(1:,k) 1))
end
In this loop i wanna accumulate my data p and write it to the cell c.
for k=1:10
c{k}=sum(p(1:,k) 1))
end
But i get the error:
c{k}=sum(p(1:,k) 1))
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched
delimiters.
What is wrong here?

回答 (1 件)

ME
ME 2020 年 4 月 4 日
編集済み: ME 2020 年 4 月 4 日

0 投票

This part of your code:
for k=1:10
c{k}=sum(p(1:,k) 1))
end
has two open (left) brackets and three close (right) brackets - so you have mismatched delimiters as suggested in your error message.

5 件のコメント

Mark S
Mark S 2020 年 4 月 4 日
編集済み: Mark S 2020 年 4 月 4 日
I have tried this but i get the same error:
for k=1:10
c{k}=sum(p(1:,k) 1)
end
ME
ME 2020 年 4 月 4 日

I think you also need a comma before the 1. The correct syntax is: sum(A,dim)

Mark S
Mark S 2020 年 4 月 4 日
編集済み: Mark S 2020 年 4 月 4 日
Ok, i have tried this:
for k=1:10
c{k}=sum(p,1)
end
But know i have this error:
Error using sum
Invalid data type. First argument must be numeric or logical.
p is in my script a cell.
ME
ME 2020 年 4 月 4 日
I’m not sure but then you probably need to swap to:
for k=1:10
c{k}=sum(p{1:,k}, 1)
end
ME
ME 2020 年 4 月 4 日
編集済み: ME 2020 年 4 月 4 日
You might just need to be careful with the data type of p. If you use ‘class()’ then you can check what data type your p commands are. You can then look up the Matlab documentation online for the acceptable classes for the sum command.

この質問は閉じられています。

タグ

質問済み:

2020 年 4 月 4 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by