How to index multiple matrices into one variable (or something comparable)
Show older comments
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
Accepted Answer
More Answers (1)
Tina M.
on 26 Sep 2011
0 votes
Categories
Find more on Matrix Indexing 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!