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