0
votes

I have a 3-Dimensional matrix K(i,j,l). I would like to create a new matrices from K, which would be a slice for each value of i. I also have to transpose the newly formed 2-D matrix.

for l=1:40  
    for j=1:15
       K1(l,j)=K(1,j,l);
       K2(l,j)=K(2,j,l);
.
.
.
       K35(l,j)=K(35,j,l);
    end;
end;

I want to create another loop where the names of new matrices are created within the loop.

i.e.;

K1(l,j)=K(1,j,l) (when i=1)
K2(l,j)=K(2,j,l) when i=2...

The problem that I faced is that I cannot seem to iteratively name of the matrices (K1,K2...K35) with in the loop and at the same time perform the dimension change operation. I tried num2str, sprintf, but they don't seem to work for some reason. If any of you have an idea, please let me know. Thanks!

2
I don't see any filename in your question. It's not very clear to me.jkshah
Sorry, there might have been a confusion. By file names I meant name of matrices K1, K2, K3 etc. My bad.Rpathy

2 Answers

1
votes

I don't understand why you want to assign different names to your matrices. Can't you just store them in a cell like this:

K = cell(35, 1);
for ii=1:35
  K{ii} = squeeze(K_DEA(ii, :, :))';
end

Otherwise, if you really need to have different names, then do this:

K = cell(35, 1);
for ii=1:35
  eval(sprintf('K%d = squeeze(K_DEA(ii, :, :))'';', ii));
end
-1
votes

If I understand your question correctly, following should solve your problem:

K1=squeeze(K(1,:,:))';
K2=squeeze(K(2,:,:))';
.
.
.
K35=squeeze(K(35,:,:))';

For looping over i=1:35

for i=1:35
  name = sprintf("K%d",i);
  A = squeeze(K(i,:,:))';
  eval([name ' = A']);
end

Or more concisely,

for i=1:35
  eval([sprintf("K%d = squeeze(K(i,:,:))'",i)]);
end