How to save these values in a matrix

hello , example : clc;clear all;close all; a=variable;b=variable2; for i=1:0.1:10 for j=1:0.1:10 ax=i-a; ay=j-b; z=[ax ay]; end end this is a very simple example but the point is i want to save all the results of ax and ay in the Z in the workspace as a matrix not the last value of ax and ay , Thanks for your help :)

댓글 수: 1

Honglei Chen
Honglei Chen 2012년 2월 28일
Please format your question
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

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

답변 (1개)

Honglei Chen
Honglei Chen 2012년 2월 28일

1 개 추천

Use a for loop and preallocate z, i.e., add
z = zeros(91*91,2)
at front and then replace the assignment to z with
clc;clear all;close all; a=1;b=2;
z = zeros(91*91,2);
i = 1:0.1:10;
j = 1:0.1:10;
for m = 1:numel(i)
for n = 1:numel(j)
ax=i(m)-a;
ay=j(n)-b;
z((m-1)*numel(i)+n,:) = [ax ay];
end
end

댓글 수: 4

Ahmed Hassaan
Ahmed Hassaan 2012년 2월 28일
well i did this and there's error saying >>>:
Subscript indices must either be real positive
integers or logicals.
Honglei Chen
Honglei Chen 2012년 2월 28일
sorry, didn't notice that you are using 0.1 step. See the updated answer above.
Ahmed Hassaan
Ahmed Hassaan 2012년 2월 28일
thx for ur attention but it gives me the same error ,,anyway this is not the code im using its just an example ,
i just want to get z in the workspace as a table containing different values of variables in the for loop ,,
and this is the code :
close all;clear all;clc
r=1;L=3;N=3;RN=10;
k=-L/2:1.732*r:L/2;
A=mean(k);
for k=-L/2:1.732*r:L/2
ax=k-A;
ay=0;
r1=sqrt(3)*r/2;r2=(r/2);x=[ax ax+r1 ax+r1 ax ax-r1 ax-r1];y=[ay+r ay+r2 ay-r2 ay-r ay-r2 ay+r2];
fill(x,y,'g')
grid on ;
hold on
plot(ax,ay,'bo')
g=[ax ay]
end
I just want g to be contained the different values of ax & ay every loop ..
and thanks again :)
Honglei Chen
Honglei Chen 2012년 2월 28일
It's because of the precision, you can either round it, or specify all possible i's and j's and then iterate the number of elements. See the updated example.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2012년 2월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by