Creating dummy variable with a nested for loop
显示 更早的评论
Hi, this is probably extremely easy but I am pretty new to MatLab so I have to ask. Here is a shortened version of what I am trying to accomplish.
Now I need to create a 6x782 (variable: d) matrix with dummies, where every companies event day each year is equal to one and the rest is equal to zero on each respective row. So every company (row) should have three one's and the rest should be zero.
The two codes below creates the matrice as I want it but they only keeps the one's for the final (3rd) year. They overwrites the two first loops.
I would really appreciate if someone could enlighten me, what have I done wrong?
Thanks
% i=company 1-6
% Q= day 1-782
% y= year 1-3
for i=1:6;
for Q=1:782;
for y=1:3;
if exd(i,y)==date1(1,Q);
d(i,Q)=1;
else d(i,Q)=0;
end
end
end
end
for Q=1:782;
for i=1:6
for y=1:3;
dummy(i,j)=logical(date1(1,Q)==exd(i,y));
end
end
end
回答(1 个)
Oleg Komarov
2012-2-29
For the first company only
ismember(date1,exd(1,:))
Preallocate and use a loop per company.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!