For cycle to create multiple matrixes
Afficher commentaires plus anciens
Hey everyone!
Im trying to create 35 5*7 matrixes, all made up by zeros and one 1 in each position.
For example, the first matrix would be
[1 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 ; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0]
the second would be
[0 1 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 ; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0] and so on!
What i tried was:
H(:,:,1)=zeros(5,7);
for n=2:36
H(:,:,n)=zeros(5,7);
for r=1:5
for c=1:7
H(r,c,n)=1;
end
end
end
but this doesnt work since I end up with 35 matrices made up by ones.
Help please! I am out of ideas!
2 commentaires
Pedro Silva
le 4 Avr 2012
Geoff
le 4 Avr 2012
What you actually want to do in this loop is turn 'n' into a single row 'r' and column 'c', and then set only that row and column to 1.
r = 1 + floor((n-1) / 5);
c = 1 + mod(n-1, 7);
Réponses (2)
the cyclist
le 4 Avr 2012
Here is a concise way to do it:
H = zeros(5,7,35);
H(1:36:end)=1;
This takes advantage of "linear indexing" to access the elements of the array as if they were in one long vector (which they are, in memory).
Also, be aware of the ability to make "sparse" arrays in MATLAB, which looks like it might be appropriate for you. See
doc sparse
for more info.
Geoff
le 4 Avr 2012
This conceptually is like turning each row of the 35x35 identity into a 5x7 matrix.
H = reshape(eye(35), 5, 7, 35);
But that's column-first precedence, and you obviously want the rows so you need to transpose a 7x5...
H = permute(reshape(eye(35), 7, 5, 35), [2 1 3]);
You can use the permute call on the cyclist's answer, which also suffers from the column-first thing.
4 commentaires
João Viveiros
le 9 Avr 2012
Hello. I found this interesting for my work at school.
I used that code you writen and it makes the one take diferent places in the matrix right?
what if you wanted for example to use N ones? For example, how would you do to have two ones in all possible combinations inside that matrix? And How would you have 3 ones in all possible combinations?
Is there a way to only change the parameter of number of ones you want in the matrix with all combinations?
Geoff
le 9 Avr 2012
Not with this solution. This simply exploits the fact that the identity matrix produced the required pattern. You'd want to start by generating all the possible permutations of your 1s and 0s into vectors. The 'perms' command won't handle the fact that most of your permutations are non-unique so I don't think you could use it. However, 'nchoosek' will tell you how many permutations you would have. The simplest approach is to generate your patterns in N loops. You could do the call to reshape in there too. I doubt there is a one-liner for what you are trying to achieve.
João Viveiros
le 10 Avr 2012
Can you just tell me how would you do that for 2 ones? if i could see a code for one one, and two ones, i think i can do the rest.
Walter Roberson
le 10 Avr 2012
You can use an "odometer" like function. See http://www.mathworks.com/matlabcentral/answers/29662-generate-points-sorted-by-distance
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!