Why isnt my Matrix being populated with values
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
This script is supposed to check if the qth value of allStateSum is between two values (p and p+1) of deltaEmatrix. If it returns true it should start populating Omega(1,p) with various sums depending on how many values q lie between p and p+1. N = 3, E = 4, deltaE = 0.5 are good values to use to test this.
I end up with just an Omega full of zeros
The script:
prompt = 'Enter Integer Value For N ';
N = input(prompt);
prompt = 'Enter Integer Value For E ';
E = input(prompt);
prompt = 'Enter Value For deltaE ';
deltaE = input(prompt);
[allState{N:-1:1}] = ndgrid(1:E); % Create N col matrix with all possible energy configurations
allState = reshape(cat(N+1,allState{:}),[],N);
allStateSum = sum(allState,2); % Sum a row of allState and place values into a col matrix
allStateSum = transpose(allStateSum);
deltaEmatrix = (0:deltaE:E+20); % create col matrix that increments by deltaE from 0 up to E
Omega = zeros(1,length(deltaEmatrix)); % Initialize Omega matrix, ready to accept values
p = 0; % The first "for loop" ascends from 1 to # of rows of deltaEmatrix
for pIndex = 1:length(deltaEmatrix)
q = 1;
x = 0;
for qIndex = 1:length(allStateSum) % Second "for loop" ascends from 1 to # of rows of allStateSum
if allStateSum(1,q) <= (p+1)*deltaE % Double "ifs" check if allStateSum(1,q) is between two values of deltaEmatrix
if allStateSum(1,q) > p*deltaE
x = x +1; % For each value it finds, increment variable x by one
Omega(1,p) = x; % Assign x to Omega(1,p)
else
continue
end
else
continue % Eventually Omega will be populated with values >= 0
end % These will be used to plot Omega vs deltaEmatrix
q = q +1;
end
p = p +1;
end
1 comentario
Voss
el 25 de Feb. de 2022
When I run the code with the suggested input values, I got one element of Omega is non-zero. If that's not what you expect to get, maybe allState/allStateSum are not being defined correctly (?).
% prompt = 'Enter Integer Value For N ';
% N = input(prompt);
% prompt = 'Enter Integer Value For E ';
% E = input(prompt);
% prompt = 'Enter Value For deltaE ';
% deltaE = input(prompt);
N = 3;
E = 4;
deltaE = 0.5;
[allState{N:-1:1}] = ndgrid(1:E); % Create N col matrix with all possible energy configurations
allState = reshape(cat(N+1,allState{:}),[],N);
allStateSum = sum(allState,2); % Sum a row of allState and place values into a col matrix
allStateSum = transpose(allStateSum);
deltaEmatrix = (0:deltaE:E+20); % create col matrix that increments by deltaE from 0 up to E
Omega = zeros(1,length(deltaEmatrix)); % Initialize Omega matrix, ready to accept values
p = 0; % The first "for loop" ascends from 1 to # of rows of deltaEmatrix
for pIndex = 1:length(deltaEmatrix)
q = 1;
x = 0;
for qIndex = 1:length(allStateSum) % Second "for loop" ascends from 1 to # of rows of allStateSum
if allStateSum(1,q) <= (p+1)*deltaE % Double "ifs" check if allStateSum(1,q) is between two values of deltaEmatrix
if allStateSum(1,q) > p*deltaE
x = x +1; % For each value it finds, increment variable x by one
Omega(1,p) = x; % Assign x to Omega(1,p)
else
continue
end
else
continue % Eventually Omega will be populated with values >= 0
end % These will be used to plot Omega vs deltaEmatrix
q = q +1;
end
p = p +1;
end
disp(Omega);
Respuestas (2)
Benjamin Thompson
el 22 de Feb. de 2022
Your code fails on this statement:
[allState{N:-1:1}] = ndgrid(1:E);
So it is unable to enter the loop to update omega. This is not correct MATLAB syntax.
2 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!