How to assign parts of one structure to another efficiently?
1 次查看(过去 30 天)
显示 更早的评论
I have a structure, 'D', which contains 'name'. I would like to assign certain parts of this struct to another struct in an efficient manner. What I have now (which works) is the following:
j = 0;
for i = 1:length(D)
if ( m_u(i) ~= 0 )
j = j + 1;
temp(j).name = D(i).name;
end
end
In this code, the contents of 'D.name' are assigned to 'temp.name' if a vector component is not zero. Is there a more efficient way to do this?
0 个评论
采纳的回答
Sean de Wolski
2014-6-19
% Sample struct
x = struct('name',cellstr(('A':'I').'))
% sample index (your mu_i)
idx = rand(9,1)>0.5
% extract
[y(1:nnz(idx)).name] = deal(x(idx).name)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!