How can I create a 10x4 matrix with a for loop?

8 visualizzazioni (ultimi 30 giorni)
I came across the following problem in my Matlab textbook: Write a loop that generates a column vector A with ten random numbers. Then create a 10 × 4 matrix having A as its fi rst column, with the remaining columns the product of the first column and the column number.
The solution they gave is the following:
for ind = 1:10;
A(ind) = rand;
end
for ind2 = 1:4;
B(1:10, ind2) = A*ind2;
end
However, although I understand each line of the code, I can't understand the logic behind the following:
1. How does the program know that 1:10 in the second for loop refers to vector A (indeed, when i run the code, the first column is vector A);
2. What sort of expression is the last one
B(1:10, ind2) = A*ind2;
i.e., shouldn't A*ind2 be in the parenthesis to indicate the other 3 columns? What does the "=" stand for here?
Also, I tried to solve it for quite a while and this is what I came up with:
A = rand(10,1);
for ii = 1:4;
ii;
x = A*ii;
B = horzcat(A,x,x,x)
end
The last command basically creates a 10x4 matrix, however, instead of creating 3 different x vectors, it just reapeats x 3 times.
Could you please tell me how to amend my code, so that it works?
Thanks a lot!! I appreciate greatly any help and comments
Silvi

Risposta accettata

dpb
dpb il 24 Ott 2014
1) It doesn't refer to A, it refers to the rows in B. (It must, because it's part of a subscripting expression for B). It has to be the same length of size(A,1) in order for the assignment to be conformant; otherwise you'd have a size mismatch.
2)
a) A*ind2 is the product of a vector by single value. Matlab does that automagically as part of the syntax of "vectorized" array calculations.
b) No, the problem statement says to make the subsequent columns the value of the first column times the column number. That's what the loop is building.
c) "=" is the assignment operator -- it assigns the RHS to the LHS target (in this case the subsequent columns of B)
In your case, you're trying to append all three columns at once but each pass thru the loop your x is one value as you overwrite the previous value of x from the subsequent loop each time thru.
A somewhat more "Matlab-y" solution could look something like
A=rand(10,1); % credit to the student, you found that one--good job!!
B=zeros(length(A),4); % preallocate so don't "grow" the array dynamically
for ii=1:4
B(:,ii)=A*ii;
end
This eliminates the specific "magic number" row indices but still uses a loop. As you progress you'll learn ways to eliminate the loop entirely.
I suggest working thru the "Getting Started" section in the online documentation--it shows all of these things and more in introducing you to Matlab.
  2 Commenti
Silviya
Silviya il 24 Ott 2014
Thanks a lot! I will definitely look into that!!
Silviya
Silviya il 25 Ott 2014
By the way, I liked your solution better than the one in the textbook. It just looks more logical to me! Thank you again!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by