How to save these values in a matrix
Mostrar comentarios más antiguos
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 comentario
Honglei Chen
el 28 de Feb. de 2012
Please format your question
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Respuestas (1)
Honglei Chen
el 28 de Feb. de 2012
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 comentarios
Ahmed Hassaan
el 28 de Feb. de 2012
Honglei Chen
el 28 de Feb. de 2012
sorry, didn't notice that you are using 0.1 step. See the updated answer above.
Ahmed Hassaan
el 28 de Feb. de 2012
Honglei Chen
el 28 de Feb. de 2012
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.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!