Matrix construction
4 views (last 30 days)
Show older comments
Francisco Romero Sánchez
on 7 Apr 2011
Hello,
I am trying to build a matrix by means of sumbmatrix. The procedure is the following: I have a loop where I get a 11x71 matrix solution and I have to repeat this loop pxq times. I want to put the first solution in the global solution matrix, in the positions (1:11,1:71), the second in (12:23,1:71) and so on to reach p times, and then the same in q to complete a (11xp,71xq) matrix.
I propose the following lines:
for p=1:9
for q=1:9
...........
loop where I get SOL(11x71)
...........
cont1=1;
cont2=1;
MATRIX([cont1:p*11,cont2:q*11])=SOL;
cont1=p*11;
cont2=q*71;
end
end
Do you know where is the problem? Would you mind to purpose a method for this problem? Thanks for your attention. Best regards
0 Comments
Accepted Answer
David Young
on 7 Apr 2011
The are a few problems with your code. One is the square brackets in the expression MATRIX([cont1:p*11,cont2:q*11]). This builds a 1-D matrix which is then used as the indexing expression - not what you want. You need to give the row and column index expressions separately, so no square brackets.
Another is the arithmetic for cont1 and cont2. These get reset to 1 just before the assignment to MATRX, so they are always 1 at this point, which again isn't what you want.
Also, if you want the second SOL matrix to go in MATRIX(12:23, 1:71), you need to make the row index change fastest - that is, the row needs to change in the inner loop.
Other points: it's better to use variables to hold values of things like the matrix sizes, because then your code becomes far more flexible, and it's a good idea to preallocate the results matrix for efficiency.
Here is a version of your code that I think does what you want. Look at how cont1 and cont2 are initialised and incremented.
nr = 11;
nc = 71;
nblocksr = 9;
nblocksc = 9;
MATRIX = zeros(nblocksr*nr, nblocksc*nc); % preallocate
cont2 = 1;
for q=1:nblocksc
cont1 = 1;
for p=1:nblocksr
SOL = rand(nr, nc); % example only
MATRIX(cont1:cont1+nr-1, cont2:cont2+nc-1)=SOL;
cont1 = cont1+nr;
end
cont2 = cont2+nc;
end
Here's another way to do it - it's more concise because it doesn't use the extra variables but just works out where to put the results directly from the indices.
nr = 11;
nc = 71;
nblocksr = 9;
nblocksc = 9;
MATRIX = zeros(nblocksr*nr, nblocksc*nc); % preallocate
for q=1:nblocksc
for p=1:nblocksr
SOL = rand(nr, nc); % example only
MATRIX((p-1)*nr+1:p*nr, (q-1)*nc+1:q*nc)=SOL;
end
end
0 Comments
More Answers (3)
Paulo Silva
on 7 Apr 2011
First you should pre-allocate memory for the matrix
MATRIX=zeros(9*11,9*71);
Now in the part you insert the solution in the matrix, it should be:
MATRIX(cont1:p*11,cont2:q*11)=SOL;
For the rest just try and watch the results.
0 Comments
philipt
on 22 Jun 2024
To construct a matrix in MATLAB, you can use a variety of methods depending on the specific requirements of your matrix. Here are some common techniques:
1. **Manual Entry:** Directly define the elements of the matrix.
```matlab
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
```
2. **Using Functions:** Create matrices using built-in functions such as `zeros`, `ones`, `eye`, and `rand`.
```matlab
B = zeros(3, 3); % 3x3 matrix of zeros
C = ones(4, 2); % 4x2 matrix of ones
D = eye(5); % 5x5 identity matrix
E = rand(3, 4); % 3x4 matrix of random numbers between 0 and 1
```
3. **Concatenation:** Combine smaller matrices or vectors into a larger matrix.
```matlab
F = [1, 2; 3, 4];
G = [5, 6; 7, 8];
H = [F, G]; % Horizontal concatenation
I = [F; G]; % Vertical concatenation
```
4. **Using a Loop:** Construct a matrix iteratively, which is useful when the elements follow a specific pattern or are computed dynamically.
```matlab
n = 4; % Size of the matrix
J = zeros(n); % Initialize an nxn matrix
for i = 1:n
for j = 1:n
J(i, j) = i + j; % Example pattern
end
end
```
5. **Special Matrices:** Use MATLAB's built-in functions for special matrices.
```matlab
K = magic(3); % 3x3 magic square matrix
L = pascal(4); % 4x4 Pascal matrix
```
Here's a complete example that demonstrates these methods:
```matlab
% Manual Entry
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
% Using Functions
B = zeros(3, 3);
C = ones(4, 2);
D = eye(5);
E = rand(3, 4);
% Concatenation
F = [1, 2; 3, 4];
G = [5, 6; 7, 8];
H = [F, G]; % [1, 2, 5, 6; 3, 4, 7, 8]
I = [F; G]; % [1, 2; 3, 4; 5, 6; 7, 8]
% Using a Loop
n = 4;
J = zeros(n);
for i = 1:n
for j = 1:n
J(i, j) = i + j;
end
end
% Special Matrices
K = magic(3);
L = pascal(4);
% Display Matrices
disp('Matrix A:');
disp(A);
disp('Matrix B:');
disp(B);
disp('Matrix C:');
disp(C);
disp('Matrix D:');
disp(D);
disp('Matrix E:');
disp(E);
disp('Matrix H (Horizontal Concatenation):');
disp(H);
disp('Matrix I (Vertical Concatenation):');
disp(I);
disp('Matrix J (Using a Loop):');
disp(J);
disp('Matrix K (Magic Square):');
disp(K);
disp('Matrix L (Pascal Matrix):');
disp(L);
```
This should cover most of the common methods for constructing matrices in MATLAB. link
0 Comments
See Also
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!