I want to use all the values present in two arrays but if I run for loops (ie) I used two for loops but only the last value of both the arrays are taken. What is the syntax so that I can use all my values of both arrays simultaneously.?

I want to use all the values present in two or three arrays but if I run for loops (ie) I used three "for" loops but only the last value of both the arrays are taken. What is the syntax so that I can use all my values of both arrays simultaneously.?
for p1= [60 78 132 12 26 10 150 156 172 168 159 174]
for p2=[61 150 16 24 25 8 155 164 173 160 171 169]
for s=[0 1 2 3 4 5 6 7 8 9 10 11]
these are the three loops...at first iteration I need 60,61,11 next iteration 78,150,1 and so on.

 Accepted Answer

V1 = [60,78,132,12,26,10,150,156,172,168,159,174];
V2 = [61,150,16,24,25,8,155,164,173,160,171,169];
V3 = [0,1,2,3,4,5,6,7,8,9,10,11];
for k = 1:numel(V1)
V1(k)
V2(k)
V3(k)
end

4 Comments

clc;
close all;
k=2;
a=(2*k)+1;
b=((2*(k*k))+(2*k)+1);
p1=[60,78,132,12,26,10,150,156,172,168,159,174];
p2=[61,150,16,24,25,8,155,164,173,160,171,169];
s=[0,1,2,3,4,5,6,7,8,9,10,11];
forj=1:numel(p1)
c=(a*p1(j))+p2(j);
f=mod(c,b);
h=s(j)-f;
t=mod(h,b);
switch(t)
case 0
x1=0;
x2=0;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 1
x1=0;
x2=1;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 2
x1=0;
x2=2;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 3
x1=-2;
x2=0;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 4
x1=1;
x2=-1;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 5
x1=1;
x2=0;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 6
x1=1;
x2=1;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 7
x1=-1;
x2=-1;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 8
x1=-1;
x2=0;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 9
x1=-1;
x2=1;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 10
x1=2;
x2=0;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 11
x1=0;
x2=-2;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
case 12
x1=0;
x2=-1;
q1=p1+x1;
q2=p2+x2;
disp (q1)
disp (q2)
break
end
end
This is my code. P1 and p2 enters correctly..but only the first value of s is taken..i want 12 values of s also. so please kindly tell what changes has to be made here.
thanks in advance.
Get rid of all of the break statements. If you want to process all elements of p1, p2, and s, then do not use break to exit the loop after the first element has been processed.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 13 Dec 2018

Commented:

on 14 Dec 2018

Community Treasure Hunt

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

Start Hunting!