Vectorization of nested for loops
Show older comments
Hi everyone,
I'm doing some practice to vectorize nested for loops.
For example, I have the following code:
a = linspace(40,45,5);
b = linspace(90,95,5);
idx = 1;
for ii = 1:4
for jj = 1:4
c(idx,:) = [a(ii),a(ii + 1),b(jj),b(jj + 1)];
idx = idx + 1;
end
end
Is there an efficient way to vectorize and run it in a much more efficiently way?
2 Comments
Dyuman Joshi
on 1 Mar 2023
What you are trying is not allowed, you are storing 4 elements in one-place holder. If you try to run your code, you will get an error.
Do you want to horizantally/vertically concetanate the elements? or do you store the each output seperately?
Angelo
on 2 Mar 2023
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!