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 ]

Respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 1 de Mayo de 2013
Editada: Azzi Abdelmalek el 1 de Mayo de 2013
S=struct('a',cellfun(@plus, {S.a},{5 6 7},'un',0))

4 comentarios

Archit
Archit el 1 de Mayo de 2013
Editada: Archit el 1 de Mayo de 2013
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 el 1 de Mayo de 2013
Editada: Azzi Abdelmalek el 1 de Mayo de 2013
S=struct('field1',cellfun(@(x,y) x^2+cos(x)+const1*x*y, {S.field1},{S.field2},'un',0))
Archit
Archit el 1 de Mayo de 2013
Editada: Archit el 1 de Mayo de 2013
as i said, i want something readable which should not be cryptic so ur two answers do not serve me
Azzi Abdelmalek
Azzi Abdelmalek el 1 de Mayo de 2013
Editada: Azzi Abdelmalek el 1 de Mayo de 2013
What do you mean by readable?
doc cellfun

La pregunta está cerrada.

Etiquetas

Preguntada:

el 1 de Mayo de 2013

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by