combined operations on struct arrays

I have a struct S
S=struct()
It has 2 fields a,b
S(1).a=2;
S(2).a=3;
S(3).a=4;
now how do i do something like
S(:).a=S(:).a+[ 5 6 7 ]

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 1 日
編集済み: Azzi Abdelmalek 2013 年 5 月 1 日

0 投票

S=struct('a',cellfun(@plus, {S.a},{5 6 7},'un',0))

4 件のコメント

Archit
Archit 2013 年 5 月 1 日
編集済み: Archit 2013 年 5 月 1 日
I want something more readable as the example i gave is very simplified.
In reality i have array of struct (having 12 fields) of 100000 elements and i have to do complicated calculation sometimes on S.field1
something on the lines of
S.field1=S.field1 .^ 2+cos(S.field1) +const1* S.field1 .* S.field2
so i am pretty sure this hack will not be useful to me. IS there a better way?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 1 日
編集済み: Azzi Abdelmalek 2013 年 5 月 1 日
S=struct('field1',cellfun(@(x,y) x^2+cos(x)+const1*x*y, {S.field1},{S.field2},'un',0))
Archit
Archit 2013 年 5 月 1 日
編集済み: Archit 2013 年 5 月 1 日
as i said, i want something readable which should not be cryptic so ur two answers do not serve me
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 1 日
編集済み: Azzi Abdelmalek 2013 年 5 月 1 日
What do you mean by readable?
doc cellfun

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

タグ

質問済み:

2013 年 5 月 1 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by