How can I add linspace in a For Loop
12 次查看(过去 30 天)
显示 更早的评论
Hello good day, I wish to have a 'for' or 'while' loop which keeps increasing the value of 'x' as the value of 'phi' keeps increasing from 0 - 77.3049 with 100 numbers. I attempted a few times but kept getting errors. Any idea how this can be implemented?
2 个评论
Light
2023-3-31
Sorry, X as the value which changes as the value of phi increases. I just cant figure out how to complete the for or while loop at the bottom.
采纳的回答
Star Strider
2023-3-31
There is not enough information (such as a description of what you want to do).
However one possibility is something like this —
phi = linspace(0, 77.3049, 100);
for k = 1:numel(phi)
x(k) = some_calculation_using(phi(k));
end
.
42 个评论
Light
2023-3-31
Okay thanks very much. I would like to declare phi as 0 to 77.3049 with 100 values.
I would also like to have a variable called X() for each of the 100 values from 0 to 77.3049.
Dyuman Joshi
2023-3-31
@Edson, this is what you are looking for.
I would suggest you to pre-allocate x
phi = linspace(0, 77.3049, 100);
%pre-allocation
x=zeros(size(phi))
for k = 1:numel(phi)
x(k) = some_calculation_using(phi(k));
end
Light
2023-3-31
Okay, thanks very much. Could you explain lines 3 with the zeros function and lines 5 with the (phi(k)); ? I dont fully understand it
Star Strider
2023-3-31
The zeros call creates a consecutive memory range that will store the resutls of the calculation. This speeds up a loop that creates new results about 20% over not prealllocating, due to the increased memory efficiency.
The ‘x(k)’ assignment is just an expression that uses the value of ‘phi(k)’ to create some result using it. It is just there for illustration purposes.
Light
2023-3-31
Okay, thank you, I understand now. I also have another question. I edited the code to have 50 values instead of 100 and I got 50 values for x, y and z. Is there any way to use the formula for l and b to calculate the values (for l and b) and plot the various values received?
Dyuman Joshi
2023-3-31
编辑:Dyuman Joshi
2023-3-31
1 - A good practice is to club all the for loop together, as their loop indices are same i.e. 1:numel(phi).
for k=1:numel(phi)
X(k)=%code;
Y(k)=%code;
Z(k)=%code;
end
2 - Use semi-colon, ; at the end of a line to suppress the outputs (such as after X, Y and Z), as I've done above.
Star Strider
2023-3-31
I am not going to type those in, since they should be provided as text, not an image.
That aside, the loops are likely not necessary. Just do something like this —
phi = linspace(0, 77.3049, 100);
x1 = ...;
x3 = ...;
y1 = ...;
y3 = ...;
z1 = ...;
z3 = ...;
X = x1*cos(phi) + x3*sin(phi);
Y = ...;
Z = ...;
b = asin(Z);
l = atan2(Y,X);
That should work, eliminating the loops and the need for preallocation. All of those results should be vectors of the appropriate dimensions. The ‘phi’ vector can be any length that produces the desired results.
.
Light
2023-3-31
@Dyuman Joshi okay I will edit the answers, thanks very much.
@Star Strider Wow okay, I got the same values as if I used the loops as well. Thank you. I used the loops because there were many values for phi.
Light
2023-3-31
Awesome, im still getting adjusted to Matlab but I learnt alot from the replies posted.
Light
2023-4-1
I have another question if its okay? We were given this code to edit to plot the path between two cities using the longitude and latitude values. I edited the code but I still keep getting errors. I dont necessarily want the answer but maybe a guide to properly edit the code?
Star Strider
2023-4-1
Copy that line exactly as written in the instructions. Changing it threw the error.
Star Strider
2023-4-1
No, you didn’t.
The 'XTick' and 'YTick' name-value pairs are on different lines in your code, and not within the parentheses.
Look again!
Light
2023-4-1
Sorry I should have stated, initially I copied the code exactly the way it was in the instructions, then I saw an error and edited the code the way it is now with the 'Xtick' and 'Ytick' in separate lines. But I also got an error after attempting to edit it.
Light
2023-4-1
It stated: "Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters."
I also tried correcting it but there was still an error.
Star Strider
2023-4-1
I only have an image of the file, not the actual text, so I can do nothing with it.
Copy the text and paste it to a Comment here.
Dyuman Joshi
2023-4-1
You need to either write the whole command in the same line or use three dots (..., Ellipsis) called at the end of each line break.
Light
2023-4-1
Okay no problem:
load('topo.mat', 'topo', 'topomap1'); % load earth topography whos topo topomap1; % set coordinates of points Longitude = 0:15:360; Latitude = -72:6:72; % set background parameters contour(0:359, -89:90, topo, [0,0], 'w'); axis equal box on set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90], 'XTick', [0:180:360], 'YTick', [-90:90:90]); %set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90]); grid; hold on % wait for more plot commands plot(Longitude, Latitude, 'y.', 'linewidth', 1); % enter plot hold off % no more plot commands to come after this line
Star Strider
2023-4-1
Please use one of the options that saves the code in a readable format. I have no strong desire to format this myuself. I will let you put in the line breaks and any other necesary formatting —
load('topo.mat', 'topo', 'topomap1'); % load earth topography whos topo topomap1; % set coordinates of points Longitude = 0:15:360; Latitude = -72:6:72; % set background parameters contour(0:359, -89:90, topo, [0,0], 'w'); axis equal box on set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90], 'XTick', [0:180:360], 'YTick', [-90:90:90]); %set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90]); grid; hold on % wait for more plot commands plot(Longitude, Latitude, 'y.', 'linewidth', 1); % enter plot hold off % no more plot commands to come after this line
.
Light
2023-4-1
Okay, do you mean save it as an m file or just structure it in the comment so it is easier to read?
Light
2023-4-1
编辑:Walter Roberson
2023-4-1
Okay
load('topo.mat', 'topo', 'topomap1');
% load earth topography whos topo topomap1; % set coordinates of points
Longitude = 0:15:360; Latitude = -72:6:72;
% set background parameters
contour(0:359, -89:90, topo, [0,0], 'w');
axis equal
box on
set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90], 'XTick', [0:180:360], 'YTick', [-90:90:90]);
%set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90]);
grid;
hold on % wait for more plot commands
plot(Longitude, Latitude, 'y.', 'linewidth', 1); % enter plot
hold off % no more plot commands to come after this line
Star Strider
2023-4-1
As posted, it runs without error —
load('topo.mat', 'topo', 'topomap1');
% load earth topography whos topo topomap1; % set coordinates of points
Longitude = 0:15:360; Latitude = -72:6:72;
% set background parameters
contour(0:359, -89:90, topo, [0,0], 'w');
axis equal
box on
set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90], 'XTick', [0:180:360], 'YTick', [-90:90:90]);
%set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90]);
grid;
hold on % wait for more plot commands
plot(Longitude, Latitude, 'y.', 'linewidth', 1); % enter plot
hold off % no more plot commands to come after this line
Now, use that with the rest of your code.
.
Light
2023-4-1
Wow, amazing thanks very much. Could I ask what caused the errors? Also in line 8 with with set(gca, 'Color' etc. Is a color supposed to be inputted there?
Star Strider
2023-4-1
My pleasure!
The errors were caused by breaking up that line so that all the name-value pair arguments were not within the argument parentheses of the set call. The original code was, as I mentioned previously, correct. The name-value pair arguments that I saw were all correct, so definitely yes.
Light
2023-4-1
Okay thanks very much, I will. Thank you for all your help. Its just one little error I get whenever I run the code, I dont know why. I didnt make any changes to it.
Star Strider
2023-4-1
You have to use continuation ellipses (...) to continue an expression over more than one line —
set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90], 'XTick', ...
[0:180:360], 'YTick', [-90:90:90]);
Testing it —
load('topo.mat', 'topo', 'topomap1');
% load earth topography whos topo topomap1; % set coordinates of points
Longitude = 0:15:360; Latitude = -72:6:72;
% set background parameters
contour(0:359, -89:90, topo, [0,0], 'w');
axis equal
box on
set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90], 'XTick', ...
[0:180:360], 'YTick', [-90:90:90]);
%set(gca, 'Color', [0 0 1], 'XLim', [0 360], 'YLim', [-90 90]);
grid;
hold on % wait for more plot commands
plot(Longitude, Latitude, 'y.', 'linewidth', 1); % enter plot
hold off % no more plot commands to come after this line
.
Light
2023-4-1
Okay so I copied the code and implemented some values but if I wish to have a line shown passing through the different points on the plot, do I have to use the polyfit function?
Star Strider
2023-4-1
I am slightly lost, since I’m not certain what you want to plot.
If you have the (x,y) coordinates of the line (whatever ‘x’ and ‘y’ actually are), just plot them using either the plot or line functions.
I believe the hold function is already implemented in the code you’re using. You will need to put any additional plot calls between the hold on and hold off calls that already exist.
Light
2023-4-1
I would like to plot a path between two cities using longitude and latitude (l and b). In the last image I attached, I plotted l vs b but there is no path or line passing through the datapoints between the two cities. I would like to have a path passing through the datapoints from one city to the next using l and b.
Star Strider
2023-4-3
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)