Matrix construction
Show older comments
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
Accepted Answer
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.
Francisco Romero Sánchez
on 7 Apr 2011
0 votes
philipt
on 22 Jun 2024
0 votes
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
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!