Azzera filtri
Azzera filtri

Why isnt my Matrix being populated with values

1 visualizzazione (ultimi 30 giorni)
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 Commento
Voss
Voss il 25 Feb 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);
Columns 1 through 38 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 39 through 49 0 0 0 0 0 0 0 0 0 0 0

Accedi per commentare.

Risposte (2)

Benjamin Thompson
Benjamin Thompson il 22 Feb 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 Commenti
Christopher Scott
Christopher Scott il 22 Feb 2022
When I run the code I dont get any errors. The other matrices, except for the Omega matrix, form like they should. Do you suspect it is that particular method (to find all permuations with repitition) that is preventing Omega from being populated? Mismatched data types? I'm a beginner matlab user so its a mystery to me.
I appreciate all your help
Christopher Scott
Christopher Scott il 22 Feb 2022
To expand on the "failed code". When i run this bit of code:
[allState{N:-1:1}] = ndgrid(1:E);
allState = reshape(cat(N+1,allState{:}),[],N);
I get a matrix of the form
allState =
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
For N = 2 E = 3 Which is what im looking for.
Im unsure why this bit of code would work for me and not others.

Accedi per commentare.


Christopher Scott
Christopher Scott il 26 Feb 2022
It finally worked. I did not know that i didnt have to specify "continue" at the bottom of each For loop. That was exiting the loop before it completed. I left it blank and now it works fine.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by