1

Is it possible to slice 3'rd line (tt)? This code is simplified, but the problem is similar. I am using multiplied iterator (3*i) in array index, however it doesn't work. Maybe it is possible to change it somehow.

parfor i = 1 : NE      
   tmp = i * [1, -1; -1, 1];                 
   tt(3*i-1:3*i+1) = tmp([3,2,4]);          
   pp(i) = tmp(1,1,i);   
end;

Thanks :)

  • It's unclear what you're trying to do. This code doesn't work as a simple `for` loop because in line 4 you're trying to access a 3rd dimension of `tmp`, but `tmp` is just a 2x2 matrix. Can you fix the code to work in a standard `for` loop? Then maybe we can see what you're trying to do and help with the `parfor`. – pancake Apr 03 '13 at 05:23
  • Sorry, that was 'making function easier' mistake. :) It should be pp() = tmp(i). – Aurimas Šimkus Apr 03 '13 at 15:20

1 Answers1

0

To be a sliced output variable, tt must be indexed using literally only the loop variable i, and other constant terms (including :). Perhaps you can make tt rectangular, and assign a whole column at a time, and then reshape later, something like this:

tt = zeros(3, 10);  
parfor ii = 1:10
  tt(:, ii) = [ii; ii; ii];
end
tt = reshape(tt, 1, numel(tt));
Edric
  • 23,676
  • 2
  • 38
  • 40
  • Thank you. Your suggestion worked out. But, still, it is strange, that it is not possible to multiply iterator in index. – Aurimas Šimkus Apr 03 '13 at 15:22
  • Unfortunately, that's one of the restrictions of PARFOR - for it to know how to slice your data, you must use the loop variable directly. – Edric Apr 04 '13 at 06:08