I'm trying to write 5 for loop cycles with a step

조회 수: 3 (최근 30일)
riki
riki 2023년 5월 19일
답변: Dyuman Joshi 2023년 5월 19일
Hello, I would like to know how could I write 5 loop cycle with a step that resolve the following problem. I have 3 vectors p1,p2,p3 that increments, from 0 to 1, with a step of 0.25. Also, I have 2 parameters (b, errore) that increments with a different step b = [0:0.4:2] ed errore = [0.4:0.3:1.5]. The sum of p1,p2,p3 must be 1 in order to get b and err parameters
I would like to have some different combination of the 5 parameters (p1,p2,p3,b,errore) so that:
  • p1 + p2 + p3 = 1 --> b = 0 , errore = 0.4 with p1=0.25, p2=0.5, p3=0.25 (it's just an example) --> then I would like to save these 5 parameters in the empty arrays.
  • p1 + p2 + p3 = 1 --> b = 0 , errore = 0.7 with p1=0.25, p2=0.5, p3=0.25
And so on for every step increment of every combination of p1,p2,p3 possible. In this case, I would get a matrix like this:
How can I do it in a smart way? I'm pretty new to MATLAB so I'm still learning
p1= [0 0 0 0];
p2= [0 0 0 0];
p3= [0 0 0 0];
b = [0 0 0 0 0 0];
errore = [0 0 0 0];
A=zeros(25,25)
for i=1:4
p1 = p1 + 0.25;
for j=1:4
p2 = p2 + 0.25;
for k=1:4
p3 = p3 + 0.25;
if p1+p2+p3 == 1
for l=1:6
b(l)=b(l)+0.4;
x(:,l)=b(l);
elseif p1(i)+p2(i)+p3(i) > 1
warning('sum exceeding 1')
else
p3 = p3 + 0.25;
end
end
end
end
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2023년 5월 19일
Do you want to save all the values of b and errore for every (p1, p2, p3) whose sum is equal to 1?
%p1 p2 p3 b errore
[0.25 0.25 0.5 0 0.4;
0.25 0.25 0.5 0 0.7;
0.25 0.25 0.5 0 1; ...
...
0.25 0.25 0.5 0.4 0.4
0.25 0.25 0.5 0.4 0.7;
0.25 0.25 0.5 0.4 1; ...
%and so on, like this
riki
riki 2023년 5월 19일
Yes exactly and for all the possible combinations of p1,p2,p3 whose sum is 1

댓글을 달려면 로그인하십시오.

채택된 답변

Andres
Andres 2023년 5월 19일
편집: Andres 2023년 5월 19일
There are many different ways to generate the desired matrix A. You may loop over two variables only and calculate the remaining p3, and generate the combinations of b and errore (that are always the same) beforehand.
% parameters to construct p<n>
pMin = 0;
pInc = 0.25;
pMax = 1;
pSum = 1; % allowed sum of p<n>
% the other two parameters
b = 0:0.4:2;
errore = 0.4:0.3:1.5;
% number of elements of each p<n>
pNum = (pMax-pMin)/pInc+1;
% number of elements of b and errore
bNum = numel(b);
eNum = numel(errore);
% number of valid p1, p2, p3 combinations (p3 = pSum-p1-p2)
pNumCom = (pNum)*(pNum-1)/2;
% number of b, errore combinations
beNumCombP = bNum*eNum;
% combine all b elements with all errore elements separately before
bE = [reshape(repmat(b, [eNum, 1]), [beNumCombP, 1]), ...
repmat(errore(:), [bNum, 1])];
% initialize A and its row index k
A = zeros(pNumCom*beNumCombP,5);
k = 0;
% loop only over p1 and p2 and calculate p3
for p1 = pMin:pInc:pMax
for p2 = pMin:pInc:(pSum-p1)
p3 = pSum-p1-p2;
P = repmat([p1, p2, p3], [beNumCombP, 1]);
A(k+1 : k+beNumCombP, :) = [P, bE];
k = k + beNumCombP;
end
end
% test:
all(sum(A(:, 1:3), 2) == pSum)
% logical
%
% 1

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 5월 19일
Vectorization ftw!
%Define variables
b = [0:0.4:2];
errore = [0.4:0.3:1.5];
%To obtain the combination according to the condition
%define p1, p2, p3 as grids
[p1, p2, p3] = meshgrid(0:0.25:1);
%Indices to for which the sum is equal to 1
idx = find(p1+p2+p3==1);
%Corresponding combinations
[E, B, IDX] = ndgrid(errore, b, idx);
IDX = IDX(:);
%Output
out = [p1(IDX) p2(IDX) p3(IDX) B(:) E(:)];
disp(out)
0 1.0000 0 0 0.4000 0 1.0000 0 0 0.7000 0 1.0000 0 0 1.0000 0 1.0000 0 0 1.3000 0 1.0000 0 0.4000 0.4000 0 1.0000 0 0.4000 0.7000 0 1.0000 0 0.4000 1.0000 0 1.0000 0 0.4000 1.3000 0 1.0000 0 0.8000 0.4000 0 1.0000 0 0.8000 0.7000 0 1.0000 0 0.8000 1.0000 0 1.0000 0 0.8000 1.3000 0 1.0000 0 1.2000 0.4000 0 1.0000 0 1.2000 0.7000 0 1.0000 0 1.2000 1.0000 0 1.0000 0 1.2000 1.3000 0 1.0000 0 1.6000 0.4000 0 1.0000 0 1.6000 0.7000 0 1.0000 0 1.6000 1.0000 0 1.0000 0 1.6000 1.3000 0 1.0000 0 2.0000 0.4000 0 1.0000 0 2.0000 0.7000 0 1.0000 0 2.0000 1.0000 0 1.0000 0 2.0000 1.3000 0.2500 0.7500 0 0 0.4000 0.2500 0.7500 0 0 0.7000 0.2500 0.7500 0 0 1.0000 0.2500 0.7500 0 0 1.3000 0.2500 0.7500 0 0.4000 0.4000 0.2500 0.7500 0 0.4000 0.7000 0.2500 0.7500 0 0.4000 1.0000 0.2500 0.7500 0 0.4000 1.3000 0.2500 0.7500 0 0.8000 0.4000 0.2500 0.7500 0 0.8000 0.7000 0.2500 0.7500 0 0.8000 1.0000 0.2500 0.7500 0 0.8000 1.3000 0.2500 0.7500 0 1.2000 0.4000 0.2500 0.7500 0 1.2000 0.7000 0.2500 0.7500 0 1.2000 1.0000 0.2500 0.7500 0 1.2000 1.3000 0.2500 0.7500 0 1.6000 0.4000 0.2500 0.7500 0 1.6000 0.7000 0.2500 0.7500 0 1.6000 1.0000 0.2500 0.7500 0 1.6000 1.3000 0.2500 0.7500 0 2.0000 0.4000 0.2500 0.7500 0 2.0000 0.7000 0.2500 0.7500 0 2.0000 1.0000 0.2500 0.7500 0 2.0000 1.3000 0.5000 0.5000 0 0 0.4000 0.5000 0.5000 0 0 0.7000 0.5000 0.5000 0 0 1.0000 0.5000 0.5000 0 0 1.3000 0.5000 0.5000 0 0.4000 0.4000 0.5000 0.5000 0 0.4000 0.7000 0.5000 0.5000 0 0.4000 1.0000 0.5000 0.5000 0 0.4000 1.3000 0.5000 0.5000 0 0.8000 0.4000 0.5000 0.5000 0 0.8000 0.7000 0.5000 0.5000 0 0.8000 1.0000 0.5000 0.5000 0 0.8000 1.3000 0.5000 0.5000 0 1.2000 0.4000 0.5000 0.5000 0 1.2000 0.7000 0.5000 0.5000 0 1.2000 1.0000 0.5000 0.5000 0 1.2000 1.3000 0.5000 0.5000 0 1.6000 0.4000 0.5000 0.5000 0 1.6000 0.7000 0.5000 0.5000 0 1.6000 1.0000 0.5000 0.5000 0 1.6000 1.3000 0.5000 0.5000 0 2.0000 0.4000 0.5000 0.5000 0 2.0000 0.7000 0.5000 0.5000 0 2.0000 1.0000 0.5000 0.5000 0 2.0000 1.3000 0.7500 0.2500 0 0 0.4000 0.7500 0.2500 0 0 0.7000 0.7500 0.2500 0 0 1.0000 0.7500 0.2500 0 0 1.3000 0.7500 0.2500 0 0.4000 0.4000 0.7500 0.2500 0 0.4000 0.7000 0.7500 0.2500 0 0.4000 1.0000 0.7500 0.2500 0 0.4000 1.3000 0.7500 0.2500 0 0.8000 0.4000 0.7500 0.2500 0 0.8000 0.7000 0.7500 0.2500 0 0.8000 1.0000 0.7500 0.2500 0 0.8000 1.3000 0.7500 0.2500 0 1.2000 0.4000 0.7500 0.2500 0 1.2000 0.7000 0.7500 0.2500 0 1.2000 1.0000 0.7500 0.2500 0 1.2000 1.3000 0.7500 0.2500 0 1.6000 0.4000 0.7500 0.2500 0 1.6000 0.7000 0.7500 0.2500 0 1.6000 1.0000 0.7500 0.2500 0 1.6000 1.3000 0.7500 0.2500 0 2.0000 0.4000 0.7500 0.2500 0 2.0000 0.7000 0.7500 0.2500 0 2.0000 1.0000 0.7500 0.2500 0 2.0000 1.3000 1.0000 0 0 0 0.4000 1.0000 0 0 0 0.7000 1.0000 0 0 0 1.0000 1.0000 0 0 0 1.3000 1.0000 0 0 0.4000 0.4000 1.0000 0 0 0.4000 0.7000 1.0000 0 0 0.4000 1.0000 1.0000 0 0 0.4000 1.3000 1.0000 0 0 0.8000 0.4000 1.0000 0 0 0.8000 0.7000 1.0000 0 0 0.8000 1.0000 1.0000 0 0 0.8000 1.3000 1.0000 0 0 1.2000 0.4000 1.0000 0 0 1.2000 0.7000 1.0000 0 0 1.2000 1.0000 1.0000 0 0 1.2000 1.3000 1.0000 0 0 1.6000 0.4000 1.0000 0 0 1.6000 0.7000 1.0000 0 0 1.6000 1.0000 1.0000 0 0 1.6000 1.3000 1.0000 0 0 2.0000 0.4000 1.0000 0 0 2.0000 0.7000 1.0000 0 0 2.0000 1.0000 1.0000 0 0 2.0000 1.3000 0 0.7500 0.2500 0 0.4000 0 0.7500 0.2500 0 0.7000 0 0.7500 0.2500 0 1.0000 0 0.7500 0.2500 0 1.3000 0 0.7500 0.2500 0.4000 0.4000 0 0.7500 0.2500 0.4000 0.7000 0 0.7500 0.2500 0.4000 1.0000 0 0.7500 0.2500 0.4000 1.3000 0 0.7500 0.2500 0.8000 0.4000 0 0.7500 0.2500 0.8000 0.7000 0 0.7500 0.2500 0.8000 1.0000 0 0.7500 0.2500 0.8000 1.3000 0 0.7500 0.2500 1.2000 0.4000 0 0.7500 0.2500 1.2000 0.7000 0 0.7500 0.2500 1.2000 1.0000 0 0.7500 0.2500 1.2000 1.3000 0 0.7500 0.2500 1.6000 0.4000 0 0.7500 0.2500 1.6000 0.7000 0 0.7500 0.2500 1.6000 1.0000 0 0.7500 0.2500 1.6000 1.3000 0 0.7500 0.2500 2.0000 0.4000 0 0.7500 0.2500 2.0000 0.7000 0 0.7500 0.2500 2.0000 1.0000 0 0.7500 0.2500 2.0000 1.3000 0.2500 0.5000 0.2500 0 0.4000 0.2500 0.5000 0.2500 0 0.7000 0.2500 0.5000 0.2500 0 1.0000 0.2500 0.5000 0.2500 0 1.3000 0.2500 0.5000 0.2500 0.4000 0.4000 0.2500 0.5000 0.2500 0.4000 0.7000 0.2500 0.5000 0.2500 0.4000 1.0000 0.2500 0.5000 0.2500 0.4000 1.3000 0.2500 0.5000 0.2500 0.8000 0.4000 0.2500 0.5000 0.2500 0.8000 0.7000 0.2500 0.5000 0.2500 0.8000 1.0000 0.2500 0.5000 0.2500 0.8000 1.3000 0.2500 0.5000 0.2500 1.2000 0.4000 0.2500 0.5000 0.2500 1.2000 0.7000 0.2500 0.5000 0.2500 1.2000 1.0000 0.2500 0.5000 0.2500 1.2000 1.3000 0.2500 0.5000 0.2500 1.6000 0.4000 0.2500 0.5000 0.2500 1.6000 0.7000 0.2500 0.5000 0.2500 1.6000 1.0000 0.2500 0.5000 0.2500 1.6000 1.3000 0.2500 0.5000 0.2500 2.0000 0.4000 0.2500 0.5000 0.2500 2.0000 0.7000 0.2500 0.5000 0.2500 2.0000 1.0000 0.2500 0.5000 0.2500 2.0000 1.3000 0.5000 0.2500 0.2500 0 0.4000 0.5000 0.2500 0.2500 0 0.7000 0.5000 0.2500 0.2500 0 1.0000 0.5000 0.2500 0.2500 0 1.3000 0.5000 0.2500 0.2500 0.4000 0.4000 0.5000 0.2500 0.2500 0.4000 0.7000 0.5000 0.2500 0.2500 0.4000 1.0000 0.5000 0.2500 0.2500 0.4000 1.3000 0.5000 0.2500 0.2500 0.8000 0.4000 0.5000 0.2500 0.2500 0.8000 0.7000 0.5000 0.2500 0.2500 0.8000 1.0000 0.5000 0.2500 0.2500 0.8000 1.3000 0.5000 0.2500 0.2500 1.2000 0.4000 0.5000 0.2500 0.2500 1.2000 0.7000 0.5000 0.2500 0.2500 1.2000 1.0000 0.5000 0.2500 0.2500 1.2000 1.3000 0.5000 0.2500 0.2500 1.6000 0.4000 0.5000 0.2500 0.2500 1.6000 0.7000 0.5000 0.2500 0.2500 1.6000 1.0000 0.5000 0.2500 0.2500 1.6000 1.3000 0.5000 0.2500 0.2500 2.0000 0.4000 0.5000 0.2500 0.2500 2.0000 0.7000 0.5000 0.2500 0.2500 2.0000 1.0000 0.5000 0.2500 0.2500 2.0000 1.3000 0.7500 0 0.2500 0 0.4000 0.7500 0 0.2500 0 0.7000 0.7500 0 0.2500 0 1.0000 0.7500 0 0.2500 0 1.3000 0.7500 0 0.2500 0.4000 0.4000 0.7500 0 0.2500 0.4000 0.7000 0.7500 0 0.2500 0.4000 1.0000 0.7500 0 0.2500 0.4000 1.3000 0.7500 0 0.2500 0.8000 0.4000 0.7500 0 0.2500 0.8000 0.7000 0.7500 0 0.2500 0.8000 1.0000 0.7500 0 0.2500 0.8000 1.3000 0.7500 0 0.2500 1.2000 0.4000 0.7500 0 0.2500 1.2000 0.7000 0.7500 0 0.2500 1.2000 1.0000 0.7500 0 0.2500 1.2000 1.3000 0.7500 0 0.2500 1.6000 0.4000 0.7500 0 0.2500 1.6000 0.7000 0.7500 0 0.2500 1.6000 1.0000 0.7500 0 0.2500 1.6000 1.3000 0.7500 0 0.2500 2.0000 0.4000 0.7500 0 0.2500 2.0000 0.7000 0.7500 0 0.2500 2.0000 1.0000 0.7500 0 0.2500 2.0000 1.3000 0 0.5000 0.5000 0 0.4000 0 0.5000 0.5000 0 0.7000 0 0.5000 0.5000 0 1.0000 0 0.5000 0.5000 0 1.3000 0 0.5000 0.5000 0.4000 0.4000 0 0.5000 0.5000 0.4000 0.7000 0 0.5000 0.5000 0.4000 1.0000 0 0.5000 0.5000 0.4000 1.3000 0 0.5000 0.5000 0.8000 0.4000 0 0.5000 0.5000 0.8000 0.7000 0 0.5000 0.5000 0.8000 1.0000 0 0.5000 0.5000 0.8000 1.3000 0 0.5000 0.5000 1.2000 0.4000 0 0.5000 0.5000 1.2000 0.7000 0 0.5000 0.5000 1.2000 1.0000 0 0.5000 0.5000 1.2000 1.3000 0 0.5000 0.5000 1.6000 0.4000 0 0.5000 0.5000 1.6000 0.7000 0 0.5000 0.5000 1.6000 1.0000 0 0.5000 0.5000 1.6000 1.3000 0 0.5000 0.5000 2.0000 0.4000 0 0.5000 0.5000 2.0000 0.7000 0 0.5000 0.5000 2.0000 1.0000 0 0.5000 0.5000 2.0000 1.3000 0.2500 0.2500 0.5000 0 0.4000 0.2500 0.2500 0.5000 0 0.7000 0.2500 0.2500 0.5000 0 1.0000 0.2500 0.2500 0.5000 0 1.3000 0.2500 0.2500 0.5000 0.4000 0.4000 0.2500 0.2500 0.5000 0.4000 0.7000 0.2500 0.2500 0.5000 0.4000 1.0000 0.2500 0.2500 0.5000 0.4000 1.3000 0.2500 0.2500 0.5000 0.8000 0.4000 0.2500 0.2500 0.5000 0.8000 0.7000 0.2500 0.2500 0.5000 0.8000 1.0000 0.2500 0.2500 0.5000 0.8000 1.3000 0.2500 0.2500 0.5000 1.2000 0.4000 0.2500 0.2500 0.5000 1.2000 0.7000 0.2500 0.2500 0.5000 1.2000 1.0000 0.2500 0.2500 0.5000 1.2000 1.3000 0.2500 0.2500 0.5000 1.6000 0.4000 0.2500 0.2500 0.5000 1.6000 0.7000 0.2500 0.2500 0.5000 1.6000 1.0000 0.2500 0.2500 0.5000 1.6000 1.3000 0.2500 0.2500 0.5000 2.0000 0.4000 0.2500 0.2500 0.5000 2.0000 0.7000 0.2500 0.2500 0.5000 2.0000 1.0000 0.2500 0.2500 0.5000 2.0000 1.3000 0.5000 0 0.5000 0 0.4000 0.5000 0 0.5000 0 0.7000 0.5000 0 0.5000 0 1.0000 0.5000 0 0.5000 0 1.3000 0.5000 0 0.5000 0.4000 0.4000 0.5000 0 0.5000 0.4000 0.7000 0.5000 0 0.5000 0.4000 1.0000 0.5000 0 0.5000 0.4000 1.3000 0.5000 0 0.5000 0.8000 0.4000 0.5000 0 0.5000 0.8000 0.7000 0.5000 0 0.5000 0.8000 1.0000 0.5000 0 0.5000 0.8000 1.3000 0.5000 0 0.5000 1.2000 0.4000 0.5000 0 0.5000 1.2000 0.7000 0.5000 0 0.5000 1.2000 1.0000 0.5000 0 0.5000 1.2000 1.3000 0.5000 0 0.5000 1.6000 0.4000 0.5000 0 0.5000 1.6000 0.7000 0.5000 0 0.5000 1.6000 1.0000 0.5000 0 0.5000 1.6000 1.3000 0.5000 0 0.5000 2.0000 0.4000 0.5000 0 0.5000 2.0000 0.7000 0.5000 0 0.5000 2.0000 1.0000 0.5000 0 0.5000 2.0000 1.3000 0 0.2500 0.7500 0 0.4000 0 0.2500 0.7500 0 0.7000 0 0.2500 0.7500 0 1.0000 0 0.2500 0.7500 0 1.3000 0 0.2500 0.7500 0.4000 0.4000 0 0.2500 0.7500 0.4000 0.7000 0 0.2500 0.7500 0.4000 1.0000 0 0.2500 0.7500 0.4000 1.3000 0 0.2500 0.7500 0.8000 0.4000 0 0.2500 0.7500 0.8000 0.7000 0 0.2500 0.7500 0.8000 1.0000 0 0.2500 0.7500 0.8000 1.3000 0 0.2500 0.7500 1.2000 0.4000 0 0.2500 0.7500 1.2000 0.7000 0 0.2500 0.7500 1.2000 1.0000 0 0.2500 0.7500 1.2000 1.3000 0 0.2500 0.7500 1.6000 0.4000 0 0.2500 0.7500 1.6000 0.7000 0 0.2500 0.7500 1.6000 1.0000 0 0.2500 0.7500 1.6000 1.3000 0 0.2500 0.7500 2.0000 0.4000 0 0.2500 0.7500 2.0000 0.7000 0 0.2500 0.7500 2.0000 1.0000 0 0.2500 0.7500 2.0000 1.3000 0.2500 0 0.7500 0 0.4000 0.2500 0 0.7500 0 0.7000 0.2500 0 0.7500 0 1.0000 0.2500 0 0.7500 0 1.3000 0.2500 0 0.7500 0.4000 0.4000 0.2500 0 0.7500 0.4000 0.7000 0.2500 0 0.7500 0.4000 1.0000 0.2500 0 0.7500 0.4000 1.3000 0.2500 0 0.7500 0.8000 0.4000 0.2500 0 0.7500 0.8000 0.7000 0.2500 0 0.7500 0.8000 1.0000 0.2500 0 0.7500 0.8000 1.3000 0.2500 0 0.7500 1.2000 0.4000 0.2500 0 0.7500 1.2000 0.7000 0.2500 0 0.7500 1.2000 1.0000 0.2500 0 0.7500 1.2000 1.3000 0.2500 0 0.7500 1.6000 0.4000 0.2500 0 0.7500 1.6000 0.7000 0.2500 0 0.7500 1.6000 1.0000 0.2500 0 0.7500 1.6000 1.3000 0.2500 0 0.7500 2.0000 0.4000 0.2500 0 0.7500 2.0000 0.7000 0.2500 0 0.7500 2.0000 1.0000 0.2500 0 0.7500 2.0000 1.3000 0 0 1.0000 0 0.4000 0 0 1.0000 0 0.7000 0 0 1.0000 0 1.0000 0 0 1.0000 0 1.3000 0 0 1.0000 0.4000 0.4000 0 0 1.0000 0.4000 0.7000 0 0 1.0000 0.4000 1.0000 0 0 1.0000 0.4000 1.3000 0 0 1.0000 0.8000 0.4000 0 0 1.0000 0.8000 0.7000 0 0 1.0000 0.8000 1.0000 0 0 1.0000 0.8000 1.3000 0 0 1.0000 1.2000 0.4000 0 0 1.0000 1.2000 0.7000 0 0 1.0000 1.2000 1.0000 0 0 1.0000 1.2000 1.3000 0 0 1.0000 1.6000 0.4000 0 0 1.0000 1.6000 0.7000 0 0 1.0000 1.6000 1.0000 0 0 1.0000 1.6000 1.3000 0 0 1.0000 2.0000 0.4000 0 0 1.0000 2.0000 0.7000 0 0 1.0000 2.0000 1.0000 0 0 1.0000 2.0000 1.3000

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by