Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problems with looping

Subject: Problems with looping

From: Lin

Date: 9 Apr, 2009 09:48:01

Message: 1 of 2

Hi all

I am stucked with the program I am writing. Can someone help me in this? From the codes, I have set it to execute 4 times. Expected results of each execution is explained in the following.

%expected result

Initially
d = {[0;1], [1;0] [1;1]}

1st execution
p{1,1} = [0;1] %assign with 1st col of d
%remove what is being used.
%d = {[1;0], [1;1]}

2nd execution
% Compare the 1st col of d (which is [1;0]) with p{1,1} to see if the
%rank ([d(:,1) p{1,1}]) == 2,
%if yes assign the col,
%if no, point to the next col and repeat the comparison

p{1,2} = [1;0]
%remove what is being used.
%d = {[1;1]}

3rd execution
p{2,1} = [0;1] --> take on the value of p{1,1}

4th execution
p{2,2} = [1;0] --> take on the value of p{1,2}

At the end of the 4 execution,
d = [1;1];
p{1,1} = [0;1]
p{1,2} = [1;0]
p{2,1} = [0;1]
p{2,2} = [1;0]

% My codes
field = 2;
[d2,d1] = ndgrid(0:field-1,0:field-1);
d = [d1(:).';d2(:).'];
d(:,1) = [];
path =[11 12 21 22];

for path_index = 1:length(path)
 
    caseVar = path(path_index);
    switch(caseVar)
        case {11 21}
            p{1,1} = d(:,1)
% d(:,1) = [];
        case {12 22}
            a = 1;
             while(rank ([p{1,1} d(:,a)])~=2)
                if (a == length(d))
                disp('No more values');
                else
                    a = a+1;
                end
                p{1,2} = d(:,a)
% d(:,a) = [];
             end
        otherwise
    end
end

It is possible to make the codes to be dynamic also so that minimum changes can be made if I am to extend the value of 'path'. Your help is greatly appreciated. Thanks.

Subject: Problems with looping

From: Lin

Date: 10 Apr, 2009 15:51:01

Message: 2 of 2

"Lin " <shenqlv@yahoo.com> wrote in message <grkg8h$5fk$1@fred.mathworks.com>...
> Hi all

> It is possible to make the codes to be dynamic also so that minimum changes can be made if I am to extend the value of 'path'. Your help is greatly appreciated. Thanks.

I posted this problem a day ago but there isnt any reply. I am not sure if what i have done previously (the switch) is feasible. Please kindly help me in this. Thanks.
Perhaps I do a liittle more elaboration. Suppose I have

d = {[0;1],[1;0],[1;1]}
path =[11 12 21 22];

11 will take the 1st col of d and stored in p{1,1}
After which the 1st col will be remove (d = {[1;0],[1;1]})

the next item in d ([1;0]) will be used to compare with p{1,1} to see if the content can form a rank of 2 if yes, then this vector will be assigned to 12 and stored it in p{1,2} (the vector will be removed), if no, point to the next col and do the comparing again.

21 will take the value of p{1,1} and the result will be stored in p{2,1}

the next item in d ([1;1]) will be used to do a comparison with p{2,1} to see if the content can form a rank of 2 if yes, then this vector will be assigned to 22 and stored it in p{2,2}(the vector will be removed.) if no, point to the next col and do the comparing again.

Result
p{1,1} = [0;1]
p{1,2} = [1;0]
p{2,1} = [0;1]
p{2,2} = [1;1]

d = [];

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us