フィルターのクリア

Why isnt my Matrix being populated with values

1 回表示 (過去 30 日間)
Christopher Scott
Christopher Scott 2022 年 2 月 22 日
回答済み: Christopher Scott 2022 年 2 月 26 日
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 件のコメント
Voss
Voss 2022 年 2 月 25 日
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

サインインしてコメントする。

回答 (2 件)

Benjamin Thompson
Benjamin Thompson 2022 年 2 月 22 日
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 件のコメント
Christopher Scott
Christopher Scott 2022 年 2 月 22 日
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 2022 年 2 月 22 日
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.

サインインしてコメントする。


Christopher Scott
Christopher Scott 2022 年 2 月 26 日
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.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by