How to run a for loop when variables are different lengths?
Show older comments
Here I have outlined two variables, delta and phi. I want to calculate beta for every possible combination of phi and delta but am not sure how since they are different lengths.
delta = -23.28:0.01:23.28;
phi = -90:1:90;
beta = phi - delta;
Accepted Answer
More Answers (2)
Image Analyst
on 7 Sep 2020
Edited: Image Analyst
on 7 Sep 2020
You can do it vectorized using meshgrid(). Try this:
delta = -23.28:0.01:23.28;
phi = -90:1:90;
[deltaAll, phiAll] = meshgrid(delta, phi);
beta = phiAll - deltaAll;
imshow(beta, [], 'XData', delta, 'YData', phi);
colormap(jet(256));
colorbar;
axis('on', 'image');
xlabel('delta', 'FontSize', 24);
ylabel('phi', 'FontSize', 24);

Sulaymon Eshkabilov
on 7 Sep 2020
Hi,
Here is the solution to your exercise:
delta = -23.28:0.01:23.28;
phi = -90:1:90;
for ii = 1:numel(delta)
for jj=1:numel(phi)
beta(ii,jj) = phi(jj) - delta(ii);
end
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!