Matrix filling with for loop
조회 수: 36 (최근 30일)
이전 댓글 표시
Hi everybody. so i'm an ultra beginner in matlab and i need your help to find a solution to the problem i have.
i'm trying to make a very simple calculations for solid state physics, it should be something like z = constant * x/y , to plot a surface graph of z, to evaluate the applicability limits of my experiment. x and y are identical vectors from 1 to 100 in steps of 1. In my mind, i think I need to build a matrix where the elements cosists in all the possible ratios between the single elements of x and y, in order to obtain my surface. I think i need to employ a for loop but i'm a bit stuck on the syntax. is there anyone that can help me? many thanks Francesco
댓글 수: 2
채택된 답변
M
2018년 1월 11일
편집: M
2018년 1월 11일
For loop documentation : https://fr.mathworks.com/help/matlab/ref/for.html?searchHighlight=for&s_tid=doc_srchtitle
f = zeros (100, 100) %I create the matrix that will lately be filled with the values
Correct, but you don't have to define your x vector to do the for loop.
If you want to loop over 100 element in step of one, you can write it like :
for i=1:1:100
for i=1:100
Both solution are the same because default step is 1.
But you should start to write the program you want to use, and then convert it into Matlab language.
댓글 수: 5
M
2018년 1월 11일
편집: M
2018년 1월 11일
Ok, it is clear now.
So, the way you initialize your matrix is ok.
f = zeros (100, 100);
what you can do, is to loop over 100 elements, for example for each row, and set each row to the desired value. Something like :
f=zeros(100,100);
for i = ... % loop over 100 elements
% i should take value 1, 2,3 ...
% then you can access ith row of f with f(:,i)
f(:,i) = ... % set the desired value for each row
% for example, your first row can be defined as
% f(:,1)=[1:100]'
end % end of for loop
f % display the result
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!