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
M 2018년 1월 11일
Did you already read the documentation ?
If yes, what have you tried and what is your problem exactly ?
Bruccio
Bruccio 2018년 1월 11일
yes, i read the documentation. i can't formalize decently the loop so that the matrix is filled with the values i refer to in my question. my current code is
f = zeros (100, 100) %I create the matrix that will lateley be filled with the values x = (1: 1: 100) %create the vectors
for w = (:, x)
for s = (:, x) f (:, x) = w/s end
of course MATAB tells me that : is not a valid my skill and experience in matlab are EXTREMELY limited and i'm still trying to figure out how coding works. i've seen a lot of tutarials and read a lot of voices, still need to figure out how to translate everything into code

댓글을 달려면 로그인하십시오.

 채택된 답변

M
M 2018년 1월 11일
편집: M 2018년 1월 11일

4 개 추천

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

Bruccio
Bruccio 2018년 1월 11일
편집: Bruccio 2018년 1월 11일
thanks for the advice. i don't know any coding language, i'm writing directly into MATLAB...i still don't get what is wrong with my code. it doesn't accept the colon operator. i know i can operate with both colums or row, in this case i'm basically telling him to go to fill the nth (1<=n<=100) with the result of the w/s operation, with w = nx. that should do the trick, but it doesn't work. chnanged my code to
for w = (1 : 1 : 100)
for s = (1 : 1 : 100)
f (:, x) = w/s
end
no error messages, but nothing happens to the matrix
M
M 2018년 1월 11일
편집: M 2018년 1월 11일
I don't really get hat you're are trying to do
i don't know any coding language : just write it in english, like :
Step 1 : create a matrix f of dimension (2,2)
Step 2 : for i=[1 2 3 4]
Compute z=1/i;
end for
Step 3 : Assign to the first element of f the first element of z
Bruccio
Bruccio 2018년 1월 11일
i want to obtain the following matrix
1 1/2 1/3 1/4 ... 1/100
2 2/2 2/3 2/4 ... 2/100
3 3/2 3/3 3/4 ... 3/100
4 4/2 4/3 4/4 ... 4/100
.
.
.
100 100/2 100/3 100/4 ... 100/100
maybe i should have written this from the beginning...sorry
M
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
Bruccio
Bruccio 2018년 1월 11일
exactly. Thanks a lot man. you're my favourite person of the week.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

질문:

2018년 1월 11일

댓글:

2018년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by