Hey guys! I am still kind of getting a hang of Matlab and I have a quick favor to ask you guys. The question is to create a double for loop for the following 3 × 4 matrix with ijth matrix element: A(i,j)=i+j, 1≤i≤3, 1≤j≤4 Construct this matrix in MATLAB using the following four methods: a. Using a double for-loop. b. Using a for-loop that builds the matrix row by row. c. Using a for-loop that builds the matrix column by column.
I have already done this but I am getting sort of an odd answer, would someone please verify my work?
i=3;
j=4;
A=zeros(i,j);
for k=1:i,
for z=1:j
A(k,z)=i+j;
end
end
A
%Part B
for k=1:i
A(k)=i+j;
end
A
%Part C
for z=1:j
A(z)=i+j;
end
A
oh and this are the arrays I got:
A =
7 7 7 7
7 7 7 7
7 7 7 7
A =
7 7 7 7
7 7 7 7
7 7 7 7
A =
7 7 7 7
7 7 7 7
7 7 7 7

 Accepted Answer

James Tursa
James Tursa on 9 Dec 2015
Edited: James Tursa on 9 Dec 2015
The reason you are getting all 7's in your answer is because i+j is always 7, so you are assigning 7 to each element. And the indexing is not correct for your second and third parts.
For the double for loop, you need to make the (k,z) element depend on the variables k and z which are used for the loop indexes, not i and j which are not used for the loop indexes. E.g.,
A(k,z) = k + z;
Had you used i and j for the loop indexes, then you could have used i + j in the above line.
For the building by row part, you need the assignment to be this for the k'th row:
A(k,:) = SOMETHING; % <-- You need to figure out how to fill in the SOMETHING
For the building by column part, you need the assignment to be this for the z'th column:
A(:,z) = SOMETHING; % <-- You need to figure out how to fill in the SOMETHING
So, I have fixed the first part for you, and partially fixed the second and third parts. You just need to figure out how to make a row of the appropriated numbers for the second part, and how to make a column of the appropriate numbers for the third part.

3 Comments

Thank you so much, yeah I figured something was way off. I'm glad you were kind enough to explain what I did wrong without making me seem like a dummy haha.
Okay so this is what I ended up doing because I could not figure out a function for A(k,:)
k=1;
A=zeros(k);
for k=1:i
A(k,1)=k+1;
A(k,2)=k+2;
A(k,3)=k+3;
A(k,4)=k+4;
end
A
Think of it this way:
A(k,:) is the k'th row, and it has "j" number of elements. The elements are as follows:
k+1 , k+2 , k+3 , ... , k+j
So how to assign those row elements all at once? In MATLAB m-code a row vector with these elements can be built as follows:
k + (1:j)
The (1:j) part will create a row vector with the elements 1,2,...,j and the k+ part will add k to each element of this row vector. So the row assignment becomes, in one line,
A(k,:) = k + (1:j);
For the building by z'th column part you will need to do something similar but constructing a column vector with the elements 1+z,2+z,...,i+z instead of a row vector. Hint: Since (1:i) is a row vector, its transpose (1:i)' is a column vector.

Sign in to comment.

More Answers (1)

John BG
John BG on 9 Dec 2015
N=3
M=4
A=zeros(N,M)
for i=1:1:N
for j=1:1:M
A(i,j)=i+j
end
end
N=3
M=4
A=zeros(N,M)
j=1
while j<M+1
for i=1:1:N
A(i,j)=i+j
end
j=j+1
end
N=3
M=4
A=zeros(N,M)
i=1
while i<N+1
for j=1:1:M
A(i,j)=i+j
end
i=i+1
end
And the MATLAB way would be:
N=3;M=4;A=zeros(N,M)
[I,J]=ind2sub(size(A),find(A==0))
A=I+J
reshape(A,N,M)

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!