필터 지우기
필터 지우기

Why isnt my Matrix being populated with values

조회 수: 2 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by