Looping through two matrices with IF statement

[EDIT: Mon May 23 16:36:16 UTC 2011 - Reformat - MKF]
Hello, I am having problems with my for loop statement (see code at bottom). I'm trying to overwrite a zeros matrix (A) with 1's if it meets certain criteria from another matrix (M). It essentially needs to look at M(:,1) and put ones in each row of A (for each numatch) according to the if conditions. Instead of putting ones in the right place, it puts in all ones up to the last numatch row. Here is the code:
%creates A
nx = 5;
ny = 6;
nullnx = zeros(nx,2);
nullny = zeros(ny,2);
nullnx(:,1)=[1:nx];
nullny(:,2)=[1:ny];
if nx >= ny;
numatch = nx;
else
numatch = ny;
end;
if nx >= ny;
x = repmat(nullnx(:,1),nx,1);
matchx = sort(x);
mx = cat(2, matchx, x);
else
x = repmat(nullny(:,2),ny,1);
matchx = sort(x);
mx = cat(2,matchx,x);
end;
M = [nullnx;nullny;mx];
A = zeros(nx+ny+numatch,nx+ny+numatch);
%here is where I need help
for i = 1:numatch;
for a = 1:length(M);
for b = 1:length(A);
if M(a, 1)== i;
A(i,b)=1;
end;
end;
end;
end;
Any help is most appreciated

2 Kommentare

Oleg Komarov
Oleg Komarov am 23 Mai 2011
Please format properly the code: http://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question
Matt Tearle
Matt Tearle am 23 Mai 2011
Please format your code.
More importantly, can you please explain what you're expecting? When I run this, I get that M is a 47-by-2 matrix and A is a 17-by-17 matrix. I can't make those numbers match in any obvious way. So please give an example of what you'd expect to see as output, or step us through your algorithm. Here's the start of M:
1 0
2 0
3 0
4 0
5 0
0 1
0 2
0 3
0 4
What should A look like for this?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Matt Fig
Matt Fig am 23 Mai 2011

0 Stimmen

Your code is doing exactly what you describe that it should do, that is it: "looks at M(:,1) and put ones in each row of A (for each numatch) according to the if condition."
Now is it the case that you need a different IF condition? If so, please show a simple example of input and expected output.
%
%
%
%
EDIT In response to your comment below.
Does this do it?
for ii = 1:numatch;
A(ii,:) = M(:,1)==ii;
end;

3 Kommentare

Sophia
Sophia am 23 Mai 2011
Hi,
Thanks for getting back. First, there is a mistake. A should be:
A = zeros(nx+ny+numatch*numatch,nx+ny+numatch*numatch);
So size(A)= [47 47]
An example is for the occurrences of 1 in M: It needs to search through M(:,1), find the 1's and put a 1 in the first row of A for the index in M where this occurs. Then it searches for 2 in M(:,1) and does the same thing for the second row of A:
i.e.,
A(1,:) =
1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00000
A(2,:) = 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 00111111000000000000000000000000000
where length(A)=47 (there may be too many zeros above). It does this for all numatch and leaves the rest as zeros. This is just to setup matrix A for linprog, such that the linear constraint sum(A(:,1))=1 and so on for cols of A.
I hope this makes more sense?
Matt Fig
Matt Fig am 23 Mai 2011
See my edit above.
Sophia
Sophia am 24 Mai 2011
That worked! How simple, thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Modeling finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by