finding a value in a column
8 次查看(过去 30 天)
显示 更早的评论
I have two separate matrices A and B. I am taking data from A and putting it into B. There is a specific order in which I can move the Data from A to B so I have to first check B to make sure the proper sequence if followed. How do I check B to see if the a value in A exists in B. For example. I have A = [14,9]of a give set of data and B= zeros[14,3]. I am taking data from A and placing it in B, but I have to move them in a certain order. I have to check conditions in columns 3-7 of A of a given row to make sure those values are already present in column 1 of B. I am trying to use the find() function but it does not seem to be working. Please keep in mind that I am very much a novice so be kind when telling me how amateur my code looks.
function LCR
format bank
a = load('Assembly I.txt');
fid = fopen('Assembly Ib.txt');
b = textscan(fid,'%s');
E = .88; %line efficiency
Ot = sum(a(:,2)) %operational time of all elements
Fp = 400; %forcast production
At = 8*60; %work shift(in minutes)
Tc = At/Fp %Time cycle
Ns = round(Ot/(Tc*E)) %Number of work stations
Dly = 100-(E*100); %Delay percentage
j = 1;
[p,q] = sort(a(:,2),'descend'); % Please look the syntax up in the help file
a(:,[1:end]) = a(q,[1:end]); % What I have done is that I have just arranged the observations in the order of their decreasing time
n = length(a);
t = Ot;
ws = 1;
station = zeros(14,3);
newt = 0;% remaining time at station
newt1 = newt;
j=1;
while n > 0
for i = 1:n;
while newt < Tc;
if a(i,2)< Tc
if all( ismember(a(i,3:7), station(:,2)) )
station(j,1)= ws;
station(j,2)= a(i,1);
station(j,3)= a(i,2);
newt = newt1 + a(i,2);
newt1 = newt;
n = n-1;
j=j+1;
break
end
end
end
ws = ws + 1;
end
end
end
2 个评论
Andrew Newell
2011-9-29
See http://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question for how to format the code.
回答(1 个)
Walter Roberson
2011-9-29
find() returns an index, but you just want to know if the value is present (I think)
It looks to me as if you could replace your 5 inner "if" tests with the single test:
if all( ismember(a(i,3:7), station(:,2)) )
9 个评论
Walter Roberson
2011-9-29
You loop for i = 1:n, and you index a(i), but inside that loop you delete rows, a(i,:)=[] . When you delete the row, that reduces the number of rows in the matrix, so if you ever actually delete anything, when i reaches n, a(i,:) will no longer exist.
In situations where you are deleting rows, it is usually better to run the loop backwards, starting from the end, so that the rows that "fall down" to occupy the removed space will be rows you have already processed, and your future processing will head towards earlier rows that have never been moved.
If you have not encountered this problem, then either you have never deleted anything or your infinite loop is "tighter" than getting around to further i values.
You appear to have tried to deal with this by decreasing n. That will not work. The "for" loop copies the range (or values) at the beginning of the loop; changes to any variables named in the "for" statement have no effect on the number of iterations.
另请参阅
类别
在 Help Center 和 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!