How to add another coloumn to a matrix that update at every iteration

4 vues (au cours des 30 derniers jours)
riki
riki le 29 Mai 2023
Réponse apportée : Dinesh le 5 Juin 2023
Hello, I'm trying to write a code that generate different functions with a certain weight. I wrote the first part thanks to @Andres but now I'm trying to do another thing. I would like to add a sixth coloumn to my matrix as I did that associate a certain solid_fraction to every p1,p2,p3 values given by the first 2 "for loop". The solid_fraction is given by M that it's a function of b,errore and all the 3 weights p1,p2,p3.
So that, for:
  • p1=0; p2=0; p3=1; b=0; errore=0.4; solid_fraction=0.1060
  • p1=0; p2=0; p3=1; b=0; errore=0.7; solid_fraction=0.1866
  • ...
  • p1=0; p2=0.25; p3=0.75; b=0; errore=0.4; solid_fraction=...
and so on.
The code is running but I can't got past Aix=25 (while I have 360 row to compute).
pMin = 0;
pInc = 0.25;
pMax = 1;
pSomma = 1;
b = 0:0.4:2;
errore = 0.4:0.3:1.5;
pN = (pMax-pMin)/pInc+1;
bN = numel(b);
eN = numel(errore);
pNumCom = (pN)*(pN-1)/2;
beNumCombP = bN*eN;
bE = [reshape(repmat(b, [eN, 1]), [beNumCombP, 1]),repmat(errore(:), [bN, 1])];
A = zeros(pNumCom*beNumCombP,6);
k = 0;
for p1 = pMin:pInc:pMax
for p2 = pMin:pInc:(1-p1)
p3 = pSomma-p1-p2;
P = repmat([p1, p2, p3], [beNumCombP, 1]);
A(k+1 : k+beNumCombP, :) = [P, bE,zeros(24,1)];
k = k + beNumCombP;
end
end
%%
!rm simulated_CT/*.*
%number of unit cells
c= 1;
%b is proportional to porosity
%number of pixels in each direction
n= 101;
%threshold
%preparazione della matrice
clear M
M=zeros((n-1),(n-1),(n-1));
solid_fraction=zeros(24,1);
%%%s,t,u parameters to induce anisotropy
s=1.;
t=1.;
u=1.;
%create the folder for the simulated CT images
mkdir('Hybrid_Stack');
Aix=1;
idx=1
while Aix<=360
for p1 = pMin:pInc:pMax
for p2 = pMin:pInc:(1-p1)
p3 = pSomma-p1-p2;
while idx<=6
iex=1;
while iex<=4
for z1=1:(n-1)
for y1=1:(n-1)
for x1=1:(n-1)
x = s*x1;
y = t*y1;
z = u*z1;
f1(x1,y1,z1)= cos(2*pi*x*c/n)*cos(2*pi*y*c/n)*cos(2*pi*z*c/n)-sin(2*pi*x*c/n)*sin(2*pi*y*c/n)*sin(2*pi*z*c/n)+b(idx); %Diamante
f2(x1,y1,z1)= 2*cos(2*pi*x*c/n)*cos(2*pi*y*c/n)+2*cos(2*pi*y*c/n)*cos(2*pi*z*c/n)+2*cos(2*pi*z*c/n)*cos(2*pi*x*c/n)-cos(4*pi*x*c/n)-cos(4*pi*y*c/n)-cos(4*pi*z*c/n)+b(idx); %IWP
f3(x1,y1,z1)= cos(2*pi*x*c/n)+cos(2*pi*y*c/n)+cos(2*pi*z*c/n)+b(idx);%Giroide
fh(x1,y1,z1)=p1*f1(x1,y1,z1)+p2*f3(x1,y1,z1)+p3*f2(x1,y1,z1); %% here select the geometry you want f1,f2,f3
f(x1,y1,z1)=fh(x1,y1,z1);
if f(x1,y1,z1)<errore(iex) && f(x1,y1,z1)>(-1*errore(iex))
M(x1,y1,z1)=1;
M_q(x1,y1)=M(x1,y1,z1);
else
M(x1,y1,z1)=0;
M_q(x1,y1)=M(x1,y1,z1);
end
end
end
clc
progress=100*z1/n;
% % filename = sprintf('image_%d.bmp', z1);
% % imwrite(M_q,filename); %creazione immagine
% % movefile(filename, 'Hybrid_stack');
end
solid_fraction(Aix) = sum(sum(sum(M)))/(n-1)^3;
A(Aix,6)=solid_fraction(Aix);
iex=iex+1;
Aix=Aix+1
end
idx=idx+1
end
end
end
end

Réponses (1)

Dinesh
Dinesh le 5 Juin 2023
Hi Riccardo!
I tried reproducing the issue on my side. I found that there is a problem in the code. After variable 'idx' becomes '7' we do not go into the while loop idx <= 6, thus we will not update the 'Aix' variable. Thus, Aix variable only updates for 25 times after that 'idx' becomes 7 and we are stuck in an infinite loop. This is the issue why the code is not working.
About how to add a column to a matrix you can simply.
A=reshape(1:16,4,4)
A = 4×4
1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
B=(17:20)'
B = 4×1
17 18 19 20
A = [A B]
A = 4×5
1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20
Refer this MATLAB documentation for more details on reshape.
Hope this helps!
Thank you.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by