Creating dummy variable with a nested for loop
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Oleg Komarov
on 29 Feb 2012
For the first company only
ismember(date1,exd(1,:))
Preallocate and use a loop per company.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!