Dear all, I have 3 cell elements and vector with 1*10 ( this is just part of 100 elements of cell). what I want elements in cell 1 equal the value.
s={[1,9,7,4][2,6,4][3,4]};
a=[ 1 0 0.5 2 1 4 5 2 3 4];
re_cell1= 1+3+5+2
re_cell2= 0+4+2
re_cell3= 0.5+2
results=[11 6 2.5];
Thanks a lot. I will appreciate it.

 採用された回答

the cyclist
the cyclist 2017 年 1 月 23 日

0 投票

Here is one way:
s = {[1,9,7,4], [2,6,4], [3,4]};
a = [ 1 0 0.5 2 1 4 5 2 3 4];
results = cell2mat(cellfun(@(x)sum(a(x)),s,'UniformOutput',false));

4 件のコメント

skysky2000
skysky2000 2017 年 1 月 23 日
You are great man. Thanks the cyclist, I really appreciate it.
Stephen23
Stephen23 2017 年 1 月 23 日
results = cellfun(@(x)sum(a(x)),s);
the cyclist
the cyclist 2017 年 1 月 23 日
skysky2000, use Stephen's simpler answer.
Originally I used the "uniform output" false, because I was returning the individual "re_cell1" vectors, which were of different lengths. Then I decided that you probably only wanted the "results" output, but forgot you could then use the simple version!
Thanks, Stephen.
skysky2000
skysky2000 2017 年 1 月 23 日
Thanks a lot, both of them work... that what I want appreciate it Stephen..

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2017 年 1 月 23 日

コメント済み:

2017 年 1 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by