finding a value in a column

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 个评论

See http://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question for how to format the code.
I do apologize for the poor formatting. I tried to change it but did not know how. Thank you for posting the link for future reference.

请先登录,再进行评论。

回答(1 个)

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 个评论

Yes, that is correct, I need to know if the value(s) is present in the second column of the station matrix. Thank you for the information. Now I try to run the code and I apparently have it in an endless loop. I think I have been looking at it too long because I just dont see where its getting caught in the loop. Would you be willing to take a look?
Please post your current code, incorporating use of ismember()
I am trying to format the code the way that link says to do it but indenting the first like of code does nothing, it still spits out the code with everything pressed to the left margin
I did what I could it still will not format to look like what was requested, my apologies. I'm basically inputting a data fill followed by a text file. I am reordering the data file in descending order per column 2. Then I want to go through the reordered [a] and sort that data based on some restrictions that are in columns 3 through 7 and fill in [station]. As a row is used from [a] I want to delete it so it is not used again. I basically want the station matrix to look like this
station element# time
1 # t
1 # t
2 # t
as the time constraints are met the first column in [station] should increment up by 1 so the following entries are in the second station and when those time constraints are met the next entries go to 3 and so on. So I am sorting elements into stations based on prerequisites per station which are identified in columns 3-7 in [a].
1 .21 0 0 0 0 0 0
2 .39 0 0 0 0 0 0
3 .70 1 0 0 0 0 5
4 .15 1 2 0 0 0 0
5 .37 2 0 0 0 0 3
6 .27 3 0 0 0 0 0
7 .63 3 0 0 0 0 0
8 .40 4 5 0 0 0 0
9 .38 5 0 0 0 0 0
10 .50 7 8 0 0 0 0
11 .11 9 0 0 0 0 12
12 .39 6 7 0 0 0 11
13 .27 10 11 0 0 0 0
14 .17 12 13 0 0 0 0
This is the data file, the call for the text file can be removed, it is for aesthetics anyway.
formatting only works in the Question or in an Answer, not in a Comment.
You could edit your Question to include the formatted code.
I'll do that
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.
What would happen if I zero the used rows? I dont know how to do what you are suggesting.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

提问:

2011-9-29

Community Treasure Hunt

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

Start Hunting!

Translated by