How to add for loop in my code to get a table of result and plot it?
3 次查看(过去 30 天)
显示 更早的评论
Hi;
My problem is that I want to have a table of result including the first coloumn is "R" (from 2 to 7) and the second one is "N_states" (for each "R" I should have a spesefic "N_states") and then plot the table. (Hint: the code (from line 3 to 11) generets just one "N_states" for one "R = 7" corectly but I want to have different "N_states" for different "R" and then plot "N_states" vers. "R")
This is my code:
for R = 2:7
%
%R = 7;
N_states = 0;
for nx = 1:R
for ny = 1:R
if ((nx.*nx + ny.*ny) <= R.*R)
N_states= N_states + 1;
end
end
end
%
R = R + 1;
end
T = table(R(:),N_states(:));
plot (T)
2 个评论
Sindar
2020-3-9
You need to store the values of N_states separately for each R. My advice would be to define R_vec outside the loop, loop over ind=1:length(R_vec), and index into R_vec and N_states with ind.
Since this is clearly a HW problem and essentially the same question has been asked many times before, I'll leave the specific coding to you
采纳的回答
Hank
2020-3-9
Yes, there are some problems with your for loop. But problems with density of states are fun!
Remember, that when you say "for R=2:7; ... end", matlab automatically increments R on every loop. You don't have to include R+1.
Also, you want to collect a table of R and N_states values, but you've only made scalar values which are getting over written on each iteration of the loop. You need to allocate an array for the R and N_states values before the loop. In this case since you're ultimately using a table, you can allocate a table.
R = (2:7)'; % allocate column vector R, integers 2,3,4..7
N_states = zeros(size(R)); % allocate N_states as zeros. This vector is the same shape as R
T = table(R,N_states); % create a table with these values
for m = 1:height(T) % loop through all the rows of T with index m
for nx = 1:T.R(m) % nx and ny range from 1 to R, the R specified by THIS row m of the table
for ny = 1:T.R(m) % are you sure you don't want to go from 0?
if ((nx.*nx + ny.*ny) <= T.R(m).*T.R(m))
T.N_states(m)= T.N_states(m) + 1; % increment this N_states in table
end
end
end
% No incrementing of R or m or nx, ny here.
end
% tables don't have an explicit plot function. Extract the vectors and plot like this:
plot(T.R,T.N_states)
Computationally there are much faster ways to solve this problem, but let me know how this feels to you.
3 个评论
Hank
2020-3-12
The idea is to make use of N-D arrays in matlab instead of using for loops. Big speed savings.
Generate a 1D array of k vectors. This will be used to apply to both dimensions. I'm using a small example with maximum k=6. Its easy to generate an array of (nx.*nx + ny.*ny) by adding the square of k to the square of its transpose
kmax = 6;
k = 0:kmax; % k between 0 and kmax
K = n.*n + n'.*n'; % matrix of R square for each ki, kj combination
A matrix results for K because you're adding a row vector to a column vector.
Now find the number of states less than R = k0, k1... kmax. Use an anonymous function and then vectorize it with arrayfun.
nStatesFun = @(R) sum(sum(K<=R));
n_states = arrayfun(nStatesFun, k)
The anonymous function finds where K <= R (returns a matrix of 1 or 0 for true or false), then sum over both matrix dimensions. The arrayfun runs the function in the first argument with each subsequent value of the array in the second argument, and returns a vector of the outputs with the same shape.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!