# How to reshape a vector without knowing the dimensions?

9 vues (au cours des 30 derniers jours)
Michael le 5 Août 2014
Réponse apportée : Michael le 6 Août 2014
Hello,
I had a 3d matrix 663x3127x254. I removed NaN's using a mask and now I assume I have less elements on the left most dimension as in vector form:
s = 526593054 x 1, i = 67510073 x 1 (this is my mask, a magnitude lower)
How can I reshape the mask vector into a 3d matrix without knowing the new dimensions?
I am familiar with the function 'reshape' but only when I know the previous dimensions.
here is my code:
i = find(~isnan(s));
s = s(i);
My ultimate goal is to use it for triscatteredinterp.
thanks, Michael
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Christopher Berry le 5 Août 2014
Using reshape with only 2 dimensions specified will work only if the number of elements is such that the 3rd dimension fits exactly. You can do better by picking the 3 dimensions yourself using factor
factor(numel(s))
Multiply these factors any way you want to produce 3 non prime values. Then use these 3 non prime factor values with reshape. For example, if numel(s) = 64 :
factor(64)
ans =
2 2 2 2 2 2
Then any of the following triples will work with reshape
(2,8,4) (4,4,4) (1,16,4)
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Plus de réponses (3)

Azzi Abdelmalek le 5 Août 2014
You needat least, know the first two dimensions, the use
reshape(i,663x3127,[])
but this is not always possible, depending of the size of i
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Sean de Wolski le 5 Août 2014
Modifié(e) : Sean de Wolski le 5 Août 2014
If you're using this for TriScatteredInterp, why do you need it as a three d array?
Couldn't you build it from a meshgrid (with corresponding nan points removed)?
[xx,yy,zz] = meshgrid(1:size(v,1),1:size(v,2),1:size(v,3));
idx = ~isnan(v);
xxv = xx(idx);
yyv = yy(idx);
zzv = zz(idx);
vv = v(idx);
T = TriScatteredInterp([xxv yyv zzv],vv)
I would also recommend using scatteredInterpolant over TriScattereredInterp if you're on a newer release.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Michael le 6 Août 2014
Thanks guys! I managed to do it!
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by