How to input an array into a matrix through looping?

I am trying to take an looped array of 10 integers, such as x= [ 2 4 .... 20] below (generated from a loop - so each will be different), rotate it (if needed), and then populate a 10:1000 matrix, keeping each 10 integers looped array output. In other words, no just repeating the output 1000x. I have been experimenting to no avail with the code below. It seems to produce the matrix correctly, but I have yet to be able to populate it with the array of 10, 1000 times across.:
clear all clc
R = 10 %number of rows
C = 1000 %number of columns
A = zeros(R,C)
x = [2 4 6 8 10 12 14 16 18 20];
z = rot90(x)
for i = 1:R
for j = 1:C
A(i,j)= z
end
end

Respuestas (2)

Stephen23
Stephen23 el 22 de Mayo de 2015
Editada: Stephen23 el 22 de Mayo de 2015
Just use repmat:
>> x = [2 4 6 8 10 12 14 16 18 20];
>> A = repmat(x(:),1,1000);
Also note that you can generate x using the colon operator:
>> x = 2:2:20;

2 comentarios

jefkei92
jefkei92 el 22 de Mayo de 2015
Thanks for the quick response, I tried to put this in and I must not be putting it in the right location, can you provide additional insight on how this gets coded into the string above?
jefkei92
jefkei92 el 22 de Mayo de 2015
Editada: jefkei92 el 22 de Mayo de 2015
I am looking to pull the array from another code which is in a loop and produces different values (new arrays of 10 each time) in each loop....I want to take these and load them into the matrix....I don't want to just generate the same numbers in each loop. Sorry I was not explicit in the original post.

Iniciar sesión para comentar.

Stephen23
Stephen23 el 22 de Mayo de 2015
Editada: Stephen23 el 22 de Mayo de 2015
Although the original question shows one vector being repeated, based on your comments to my first answer and the edited question you are actually trying to allocate different vectors with each iteration. The two things you need to do are:
Note that the complete vector (its orientation does not matter) can be allocated using colon notation, like this:
A = zeros(10,1000);
for k = 1:1000;
vector = ... calculations here!
A(:,k) = vector;
end

2 comentarios

Ok...getting there....really appreciate the help, but not quite there... built the following to show what I am looking for...
See below..
clear all
clc
% T = trial run
x = [2 4 6 8 10 12 14 16 18 20];
A = zeros(10,1000);
for k = 1:1000;
T = x*rand()
A(:,k) = T;
[mx,r] = max(T)
t = tabulate(r)
end
This code is individually tabulating "r" for each loop, but I need to output a table which can give me data for all 1000 runs or total "r's"...can you help?
Stephen23
Stephen23 el 25 de Mayo de 2015
Editada: Stephen23 el 25 de Mayo de 2015
You could move the max and tabulate outside of the loop, if you want them to be performed on the entire matrix. Currently the code does not make much sense, as the scalar r is going to be them same value on every iteration, and calling tabular on a scalar is anyway pointless.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 22 de Mayo de 2015

Editada:

el 25 de Mayo de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by