0

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!

Rpathy
  • 3
  • 3
  • I don't see any filename in your question. It's not very clear to me. – jkshah Oct 26 '13 at 14:21
  • Sorry, there might have been a confusion. By file names I meant name of matrices K1, K2, K3 etc. My bad. – Rpathy Oct 26 '13 at 14:33
  • 2
    See: http://stackoverflow.com/questions/15438464/matlab-changing-the-name-of-a-matrix-with-each-iteration/15439046#15439046 – Pursuit Oct 26 '13 at 14:55

2 Answers2

1

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
  • `squeeze` is needless. – jkshah Oct 26 '13 at 15:25
  • I don't understand what user2923076 and jkshah are doing. 1. K(1,:,:) is a 3D array and it cannot be transposed in Matlab: they say jkshah's code works, I don't think so, it definitely doesn't on versions of Matlab earlier than R2010b. That's why you need squeeze. 2. Matlab strings need '', not "" like in jkshah's code. The latter produces errors, at least on versions of Matlab earlier than R2010b. Have Matlab new versions updated these features? –  Oct 26 '13 at 17:14
  • 1. As far as I know `K(1,:,:)` is **2D** matrix not **3D**. 2. Regarding syntax, I generally [compile online](http://www.compileonline.com/execute_matlab_online.php), though I haven't tested it on MatLab. As long as it works for OP, it should be fine. – jkshah Oct 26 '13 at 17:25
  • Try K(1, :, :)' and it will return error: «??? Error using ==> ctranspose / Transpose on ND array is not defined». This happens also on the website that you suggested (which anyway compiles in Octave). So I am a bit puzzled. –  Oct 26 '13 at 17:28
  • Oops, sorry. You're true, my mistake. I might not have tested for 3D. For sake of simplicity I just tried 2D for testing, my bad. I have added `squeeze` to my solution. Now I am puzzled, how OP accepted it??!! He should have tested on his real data – jkshah Oct 26 '13 at 17:44
  • Actually I thought «Hey! Did they change anything in Matlab lately?» :-) –  Oct 26 '13 at 17:48
  • I suppose not! bdw, your idea of storing in a cell is nice one. I am still puzzled what OP's doing! We may not get enough chance to test our solution, but he must have tested on his read data! – jkshah Oct 26 '13 at 17:52
-1

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
jkshah
  • 11,387
  • 6
  • 35
  • 45
  • I am currently doing that, but I was wondering whether the changing values (1..35) can themselves be a part of a loop. That is, I need the matrix names to change within the loop. Since I have to do the same for quite a few matrices, it would be a bit cumbersome to do have too many lines. – Rpathy Oct 26 '13 at 14:54
  • @user2923076 Please note the addition of `squeeze` function to my solution. – jkshah Oct 26 '13 at 17:44