Change dimension of a matrix

조회 수: 314 (최근 30일)
Tinkul
Tinkul 2012년 9월 14일
답변: Ilayat Ali 2023년 9월 13일
How to change a matrix dimension 1X131072 to 256X256
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 14일
do you mean 256*512
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 14일
편집: Azzi Abdelmalek 2012년 9월 14일
131072/256=512 some data will be missing

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 9월 14일
편집: Andrei Bobrov 2012년 9월 14일
reshape(yourmatrix,256,256,[]);
or
permute(reshape(yourmatrix,256,256,[]),[2 1 3]);

추가 답변 (3개)

Wayne King
Wayne King 2012년 9월 14일
편집: Wayne King 2012년 9월 14일
You cannot because the number of elements will change. 131072 is not equal to 256^2 so what are you going to do with all the left over elements? You can choose a subset of the 131,072 elements in your vector to create a 256x256 matrix.
x = randn(131072,1);
y = x(1:65536);
y = reshape(y,256,256);
You can reshape it to 256x512, or 512x256 using reshape.

Image Analyst
Image Analyst 2012년 9월 14일
You can change the number of elements if you want. If you have the Image Processing Toolbox, you can use imresize()

Ilayat Ali
Ilayat Ali 2023년 9월 13일
You can change the dimensions of a matrix using various functions and techniques, depending on your specific requirements. Here are some common ways to modify the dimensions of a matrix:
1. Reshaping a Matrix: You can change the dimensions of a matrix without changing its data by using the reshape function. This function rearranges the elements of the matrix while preserving their values. Here's how to use it:
A = [1, 2, 3; 4, 5, 6];
B = reshape(A, 3, 2); % Reshape A into a 3x2 matrix
2. Transposing a Matrix: You can change the dimensions of a matrix by transposing it. This swaps the rows and columns. Use the ' operator for matrix transpose:
A = [1, 2, 3; 4, 5, 6];
B = A'; % Transpose A, changing it from a 2x3 matrix to a 3x2 matrix
3. Slicing: You can extract submatrices from a larger matrix to change its dimensions. For example, to extract a specific subset of rows and columns:
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
B = A(1:2, 1:2); % Extract a 2x2 submatrix from A
4. Concatenation: You can concatenate matrices to change their dimensions. For example, to horizontally concatenate two matrices:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = [A, B]; % Concatenate A and B horizontally, resulting in a 2x4 matrix
5. Resizing: You can change the size of a matrix using functions like zeros, ones, or rand. For example, to change a matrix A to a larger size while keeping its data:
A = [1, 2; 3, 4];
newSize = [3, 3]; % New size
B = zeros(newSize); % Create a new matrix with zeros
B(1:size(A, 1), 1:size(A, 2)) = A; % Copy the data from A to B
These are some common techniques for changing the dimensions of a matrix in MATLAB. The method you choose depends on your specific data manipulation needs.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by