From: Arwel on 4 Feb 2010 13:18 Hi, My issue is best served with an example. Suppose I have.... a{1}.val = 1 a{2}.val = 2 a{3}.val = 3 Is there a quick way of doing this without using a loop... for i = 1:3 atot{i} = a{i}.val end i.e., something like.... atot = a{1:3}.val... (which doesn't work) Arwel
From: Steven Lord on 4 Feb 2010 13:31 "Arwel " <a.v.hughes(a)rl.ac.uk> wrote in message news:hkf30s$oen$1(a)fred.mathworks.com... > Hi, > My issue is best served with an example. > Suppose I have.... > > a{1}.val = 1 > a{2}.val = 2 > a{3}.val = 3 > > Is there a quick way of doing this without using a loop... > > for i = 1:3 > atot{i} = a{i}.val > end S = [a{:}]; atoi = {S.val}; -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: Walter Roberson on 4 Feb 2010 13:33 Arwel wrote: > Hi, > My issue is best served with an example. > Suppose I have.... > > a{1}.val = 1 > a{2}.val = 2 > a{3}.val = 3 > > Is there a quick way of doing this without using a loop... > > for i = 1:3 > atot{i} = a{i}.val > end > > i.e., something like.... atot = a{1:3}.val... > (which doesn't work) > Arwel p = cellfun(@(v) v.val, a, 'uniform',0); [atot{1:length(a)}] = deal(p{:}); The loop is still there, but it is hidden inside cellfun. It would be easier of your 'a' was a plain struct array rather than a cell array. e.g., a(1).val = 1; a(2).val = 2; a(3).val = 3; [atot{1:length(a)}] = deal(a.val);
From: Walter Roberson on 4 Feb 2010 13:42 Steven Lord wrote: > "Arwel " <a.v.hughes(a)rl.ac.uk> wrote in message > news:hkf30s$oen$1(a)fred.mathworks.com... >> Suppose I have.... >> a{1}.val = 1 >> a{2}.val = 2 >> a{3}.val = 3 >> >> Is there a quick way of doing this without using a loop... >> >> for i = 1:3 >> atot{i} = a{i}.val >> end > S = [a{:}]; > atoi = {S.val}; Unfortunately, that doesn't work, Steve. a{1} is a struct, so S will be a 1 x 3 cell array of struct. You cannot access a cell array of struct according to field name. Hence my cellfun solution.
From: Arwel on 4 Feb 2010 16:12
Thanks! Much obliged for your help. It may be possible to change the code so that 'a' is a struct rather than a cell... But if not... is the 'hidden' loop in cellfun faster than the simple for loop??/ (oh... actually, I'll just try it myself with tic and toc and let you know). Cheers, Arwel Walter Roberson <roberson(a)hushmail.com> wrote in message <hkf4da$idl$1(a)canopus.cc.umanitoba.ca>... > Arwel wrote: > > Hi, > > My issue is best served with an example. > > Suppose I have.... > > > > a{1}.val = 1 > > a{2}.val = 2 > > a{3}.val = 3 > > > > Is there a quick way of doing this without using a loop... > > > > for i = 1:3 > > atot{i} = a{i}.val > > end > > > > i.e., something like.... atot = a{1:3}.val... > > (which doesn't work) > > Arwel > > p = cellfun(@(v) v.val, a, 'uniform',0); > [atot{1:length(a)}] = deal(p{:}); > > The loop is still there, but it is hidden inside cellfun. > > It would be easier of your 'a' was a plain struct array rather than a cell > array. e.g., > > a(1).val = 1; a(2).val = 2; a(3).val = 3; > [atot{1:length(a)}] = deal(a.val); |