필터 지우기
필터 지우기

How to get rid of the for loop ?

조회 수: 1 (최근 30일)
Robert Thiel
Robert Thiel 2015년 9월 9일
댓글: Robert Thiel 2015년 9월 10일
the task
handles.plotdata.IR1 = zeros(1, 2001);
handles.plotdata.IR2 = zeros(1, 2001);
handles.plotdata.ax = zeros(1, 2001);
handles.plotdata.ay = zeros(1, 2001);
handles.plotdata.az = zeros(1, 2001);
my simplification so far
Kanal_name = {'IR1' 'IR2' 'ax' 'ay' 'az'};
for n = 1:length(Kanal_name)
handles.plotdata.(Kanal_name{n}) = zeros(1, 2001);
end
now i want to get rid of the for loop, any suggestions ?
  댓글 수: 1
James Tursa
James Tursa 2015년 9월 9일
Why do you want to get rid of the loop?

댓글을 달려면 로그인하십시오.

채택된 답변

Kelly Kearney
Kelly Kearney 2015년 9월 9일
Maybe this?
tmp = cell(2,length(Kanal_name));
tmp(1,:) = Kanal_name;
[tmp{2,:}] = deal(zeros(1,2001));
handles.plotdata = struct(tmp{:});
But this makes the code more difficult to read without adding any real benefit that I can see (the time difference is pretty negligible). Unless you have a definitive need to eliminate loops, I'd stick with your version.
  댓글 수: 2
per isakson
per isakson 2015년 9월 9일
... or this
>> clear all
>> handles.plotdata = cell2struct( repmat({zeros(1, 2001)},1,5) ...
, {'IR1' 'IR2' 'ax' 'ay' 'az'}, 2 );
>> handles.plotdata
ans =
IR1: [1x2001 double]
IR2: [1x2001 double]
ax: [1x2001 double]
ay: [1x2001 double]
az: [1x2001 double]
Robert Thiel
Robert Thiel 2015년 9월 10일
Thank you very much, both of you. Helps a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by