Can you vectorize this code?

5 views (last 30 days)
Bryana
Bryana on 15 Apr 2014
Edited: Jan on 15 Apr 2014
This comes from the Matlab book and I'm having the hardest time understanding it.
Chapter 5, problem 35
Vectorize this code:
n = 3;
x = zeros(n);
y = x;
for i = 1:n
x(:,i) = i;
y(i,:) = i;
end
Please help!

Accepted Answer

lvn
lvn on 15 Apr 2014
Edited: lvn on 15 Apr 2014
n=3;
x=meshgrid(1:n,1:n);
y=x';

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 15 Apr 2014
Edited: Azzi Abdelmalek on 15 Apr 2014
Use repmat function

Categories

Find more on Resizing and Reshaping Matrices 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!