How to index multiple matrices into one variable (or something comparable)
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I'm super new to Matlab. My first language is Python and this is my second but I'm still new to programming overall so please excuse my mistakes.
Basically I'm editing someone else's script for my own purposes. (The author of the script basically sent me an angry 'don't bother me' note so I can't ask him.)
His script contains a function which returns a matrix/array in a for loop, passing it to another for loop and recalculating the matrix for a particular "mode." I am separating that step from the proceeding step and trying to calculate all of the matrices and store them into one variable.
In python you can make lists of lists, i.e. y = [[1,2] , [2,4,5]] and y[1]=[2,4,5]. (I know the indexing init is different). Can you do the same in matlab? How would you do that?
Here is his original code:
% mode defines the current eigenmode we want to visualize
for mode = 1:numPCsToKeep
eigenmode = eigenvecs(:,mode); % eigenmode direction
variance = eigenvals(mode); % data variance in the current eigenmode
standev = sqrt(variance);
for i=-2:2
% shift along the current eigenmode direction
cfvec = mfvec + eigenmode'*i*standev;
% visualize the current fvec
cfvec = reshape(cfvec,3,256)';
[spharm_vertices, spharm_faces] = surf_spharm(cfvec,dg,meshsize);
% subplot(4,5,(mode-1)*5+i+3); patch_lighta(spharm_vertices, spharm_faces); axis off;
% title(sprintf('%d*std',i));
new_name = sprintf('PC%d_%d',mode,i);
% make surface based on mesh picked
switch outputFormat % format to use for output
case 'Amira'
outputToAmira(currentDir, new_name, spharm_vertices, spharm_faces);
case 'STL'
outputToSTL(currentDir, new_name, spharm_vertices, spharm_faces);
otherwise % Amira output is default
outputToAmira(currentDir, new_name, spharm_vertices, spharm_faces);
end
end
end
Here is my code:
evcounter=1;
eigenmode=[];
variance=[];
standev=[];
for mode = 1:numPCsToKeep
eigenmode(evcounter) = eigenvecs(:,mode); % eigenmode direction
variance(evcounter) = eigenvals(mode); % data variance in the current eigenmode
standev = sqrt(variance);
evcounter = evcounter + 1;
end
% written for 3 PCs to be used even though numPCsToKeep allowed to be picked. Won't work if mode < 3 is to be used.
for a=[-2 -1.5 -1 -0.5 0 0.5 1 1.5 2] % PC1
for b=[-2 -1.5 -1 -0.5 0 0.5 1 1.5 2] % PC2
for c=[-2 -1.5 -1 -0.5 0 0.5 1 1.5 2] % PC3
% shift along the current eigenmode direction
cfvec = mfvec + eigenmode(1)'*a*standev(1) + eigenmode(2)'*b*standdev(2) + eigenmode(3)'*c*standdev(3);
% visualize the current fvec
cfvec = reshape(cfvec,3,256)';
[spharm_vertices, spharm_faces] = surf_spharm(cfvec,dg,meshsize);
% subplot(4,5,(mode-1)*5+i+3); patch_lighta(spharm_vertices, spharm_faces); axis off;
% title(sprintf('%d*std',i));
new_name = sprintf('%dPC1_%dPC2_%PC3',a,b,c);
% make surface based on mesh picked
switch outputFormat % format to use for output
case 'Amira'
outputToAmira(currentDir, new_name, spharm_vertices, spharm_faces);
case 'STL'
outputToSTL(currentDir, new_name, spharm_vertices, spharm_faces);
otherwise % Amira output is default
outputToAmira(currentDir, new_name, spharm_vertices, spharm_faces);
end
end
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 26 Sep. 2011
If you want something list-like, use cell arrays. Your python
y = [[1,2] , [2,4,5]]
would translate to
y = {[1,2], [2,4,5]};
and then
y{1} would be [1,2] and y{2} would be [2,4,5]
Note that in Matlab, [2,4,5] is a numeric array of dimension 1 x 3, also known as a row vector. Numeric vectors are indexed with () subscripts, such as
t = y{2};
t(3)
with the t(3) being the third element of the numeric vector [2,4,5], which would be the scalar 5 . On the other hand, scalars in MATLAB are numeric arrays of dimension 1 x 1 -- the datatype for scalars and arrays is the same, so 5 is the same as [5]
This differs from t = {2,4,5} which will be a cell array of dimension 1 x 3. t(3) in this case (note the '()' indexing) would be the cell array {[5]} which is a cell array, not a scalar. t{3} would be the "content" of t(3) and would thus be [5], a scalar. Cell arrays may have cells that contain data of different size or data type than other cells: for example, z={'hello',[2,4,5]} is valid. z(1) would be {'hello'}, a cell containing a string; z{1} would be the string 'hello' itself; z(2) would be {[2,4,5]}, a cell containing a numeric array; z{2} would be [2,4,5], the numeric array itself. z{1}(2) would be 'e' and z{2}(2) would be 4. The distinction starts to matter more when you have more layers of cells -- cell arrays may contain cell arrays.
2 Kommentare
Walter Roberson
am 27 Sep. 2011
What kind of transformation would you like to do to the elements of the cell array? Generally speaking, cellfun() would be used, probably with the 'Uniform', 0 option added
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!