Info
This question is closed. Reopen it to edit or answer.
help with if conditional statement
    2 views (last 30 days)
  
       Show older comments
    
I have the following code
a1=1:10; a2=11:20; a3=21:30; a4=31:40; a5=41:50; a6=51:60; a7=61:70; a8=71:80;
a9=81:90; a10=91:100; a11=101:110; a12=121:130;
max_runs = [4,6,2,4,1,4,3,6,4,3,8,2];
b1=cell(1,12); 
b1{1}=a1;b1{2}=a2;b1{3}=a3;b1{4}=a4;b1{5}=a5;b1{6}=a6;
b1{7}=a7;b1{8}=a8;b1{9}=a9;b1{10}=a10;b1{11}=a11;b1{12}=a12;
P=2;    b2=cell(1,P);
j=0;
mat = zeros(P,1);
while 1
    j=j+1;
    b2(1:P)=b1(1:P);
    for i=1:P
            mat(i,j)=b2{i}(randi(numel(b2{i}),1,1));
    end
    if j==30
        break
    end
end
mat
which returns random picks from the selected vectors from the cell
2     1     9     1     3     5     2     2     9     6     9     4     5     3     2     5    10     5    4     4     8     3     1    10     6     3     9     1     7     7
20    18    19    14    19    20    13    12    16    12    17    16    11    12    13    11    20    15     20    12    14    15    12    20    11    14    11    12    18    15
I want to create the following condition. 'j' corresponds to the columns of 'mat', 'max_runs' corresponds the maximum number of columns each vector should run over when filling 'mat'. In 'mat' output, on the for the 1st row, after column 4, I want to b2(1) with b1(3). For the 2nd row, after column 6, I want to replace b2(2) with b1(4) and keep going like that, that for the 1st row again, after 2 columns, b2(1)=b1(5) and for 2nd row again, after 4 columns b2(2)=b(6).
The result should look something like this since im picking random numbers
2 1 9 1                          21 24                 46                               65 70 67
20 18 19 14 19 20        31 35 36 33       52 54 51 56 60 56      71 78 72 80 75 71
0 Comments
Answers (1)
  Bob Thompson
      
 on 10 Apr 2019
        I don't really understand all of what you're looking to do, but perhaps I can give some basic outline of how you can set up the first couple of conditions.
'In 'mat' output, on the for the 1st row, after column 4, I want to b2(1) with b1(3).'
for j = ...
    for i = ...
        if i == 1 & j > 3 & j < 6
            b2(i) = b1(4);
            mat(i,j)=b2{i}(randi(numel(b2{i}),1,1));
        elseif i == 2 & j > 5 & j < 10
            b2(i) = b1(4);
            mat(i,j)=b2{i}(randi(numel(b2{i}),1,1));
        elseif
            ...
        end
    end
end
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
