how to reshape an n x 1 matrix into a squre matrix using matlab?

hi experts........... greetings to all. hear a challenge question? how to reshape an n x 1 matrix into a square matrix using matlab?please let me know.i would be grateful to you.i am waiting for solution.
thanks in advance

 채택된 답변

Jos (10584)
Jos (10584) 2011년 3월 4일
This function will take any vector (or even a matrix) V and reshapesit into the smallest square matrix that can hold it. Note that it does not use reshape.
function M = reshapeVintosquareM (V)
% Reshapes a vector into the smallers square matrix possible. If V is too short
% the remainder of M is filled with NaNs.
N = numel(V) ;
M = NaN(ceil(sqrt(N))) ;
M(1:N) = V ;

댓글 수: 3

Update: this approach is basically the same as WR's solution, of course.
Yep, there are special-cases of my code for some specific fill values such as nan:
SQ = nan(ceil(sqrt(length(vec)))); SQ(1:length(vec)) = vec;
thanks my friend

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

추가 답변 (4개)

Andreas Goser
Andreas Goser 2011년 3월 3일
The answer is already in your question. RESHAPE.
x=[1 2 3 4 5 6 7 8 9];
reshape(x,3,3)

댓글 수: 6

thanks for your reply.
i have an n x 1 matrix
Well 1xn / nx1, what does it matter? The code also works with reshape(x',3,3)
A patient answer for an easy problem. +1
what if n is a prime?
e.g. n=7,11,13,19....
after reshaping product of dimensions should be same.
i mean, i have an input n x 1 matrix, so n*1 should be equal to m*m,where m x m is the dimension of output square matrix and m<n.
in your example you have taken 1 x 9.we can write 1*9=3*3,so you could reshape the matrix 1 x 9 as 3 x 3 square matrix.
Sorry, Matlab is not able to handle that situation because Matlab uses finite precision arithmetic, and the situation you outline requires infinite precision arithmetic in order to properly be able to distinguish between indices 4.1231056256176605498214098559740770251471992253736, sqrt(17), and 4.1231056256176605498214098559740770251471992253737
thanks for your awesome response

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

Walter Roberson
Walter Roberson 2011년 3월 4일

2 개 추천

Vectors which are not the square of a positive integer can be accomodated, if the unused spaces can be filled with some specific value of the same data class as the array values.
SQ = FillValue * ones(ceil(sqrt(length(vec)))); SQ(1:length(vec)) = vec;
The above will also work for vectors which would form perfect squares.

댓글 수: 2

What an excellent service. You get ultimative solutions even for very basic questions.
I hope that Ramesh is goind to accept an answer.
thanks dude

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

David Young
David Young 2011년 3월 3일

0 개 추천

The SQRT function may be useful here.

댓글 수: 4

hi david,
sqrt won't work.it works only when n is a perfect square.what,if n is not a perfect square?
Then there's no way to do what you ask in the original question -reshape n-by-1 into a square matrix.
can we do that by adding some dummy rows and columns?
Adding dummy rows or columns would be contrary to the requirement that "after reshaping product of dimensions should be same".
If n is composite, it would be possible to arrange n in to a rectangle and add dummy rows and columns such the the result was a square matrix. If n is prime, then you would have to add (n-1) dummy rows (or columns) of length n in order to form a square.
I would have to think a bit to come up with an algorithm for determining the fewest total dummy rows and columns necessary to create the square matrix. I suspect that might also be the same solution as "the lowest total number of dummy elements added" but I am too tired at the moment to prove it mentally.
In the above discussion, I am assuming that n must be arranged in to a rectangle. The answer I gave above with SQ is based upon arranging n in to a square that has just a single partial strip of elements missing and filling those missing elements with a dummy value.

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

Paulo Silva
Paulo Silva 2011년 3월 4일
function [SQmatrix,flag]=vec2SQmat(vec)
%[SQmatrix,flag]=vec2SQmat(matrix)
%
%This function receives a vector and tries
%to convert it to a square matrix SQmatrix
%flag indicates the sucess (1) or failure (0)
%
flag=0;
sq=sqrt(numel(vec));
if (round(sq)==sq)
SQmatrix=reshape(vec,sq,sq);
flag=1;
end

카테고리

도움말 센터File Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by