Clear Filters
Clear Filters

How to extract a page from a 3d array and add it to a 2d array in a for loop?

2 views (last 30 days)
I need to form a matrix "S" (22by22) using pages from a multidimensional array "s" in a for loop. First, this is the example how the "S" matrix is being built when the multidimensional array "s" is a 2d array (4by4):
a = 22;
b = [1,2;2,3;3,4;4,5;5,6;6,7;7,8;8,9;9,10;10,11];
c = 10;
s = rand(4,4);
S = zeros(a);
for i = 1:c;
indice = b(i,:);
e = [2*(indice(1)-1)+1 2*(indice(2)-1) 2*(indice(2)-1)+1 2*(indice(2)-1)+2];
S(e,e) = S(e,e) + s;
end
Now, all the variables stay the same just matrix "s" is a multidimensional array (4,4,10). So, I need to follow the same approach of building matrix "S" but just using the pages from matrix "s". I tryed to do this:
a = 22;
b = [1,2;2,3;3,4;4,5;5,6;6,7;7,8;8,9;9,10;10,11];
c = 10;
s = rand(4,4,10);
S = zeros(a);
for i = 1:c;
indice = b(i,:);
e = [2*(indice(1)-1)+1 2*(indice(2)-1) 2*(indice(2)-1)+1 2*(indice(2)-1)+2];
S(e,e) = S(e,e) + s(:,:,i);
end
But it is not working. I also tried to reshape the "s" into 2d (40by4) but then I don't know how to the for loop for obtaining "S". By reshaping "s" into 40x4, in the for loop for i=1 I need to take elements from "s" (1:4,:) and add this to "S", for i=2 I need to take s(5:8,:) and add it to "S", for i=3, I need to take s(9:12,:) and add it to "S" etc.
Any advice how to solve this would help a lot.
Thank you!

Answers (1)

dpb
dpb on 7 Jan 2016
Firstly, the indices vector e and subsequently S can be built from
L=4;
for i=1:2:c-(L-1)
e=i:i+L-1;
...
for the 2D case.
For the 3D case you simply need to keep track of how many iterations you've made and get the next --
j=0; % counter for plane to use
for i=1:2:c-(L-1)
e=i:i+L-1;
j=j+1; % increment plane counter
S(e,e)=S(e,e)+s(:,:,j);
end
I've not thought about it but likely you can manage to do this via filter2d but for small sizes such as this the loop will likely be "fast enough" unless it is also buried in a deeply nested construct.
  3 Comments
dpb
dpb on 8 Jan 2016
"... j isn't changing, it is always taking the j=1 for every i"
Not unless you've made a typo somewhere it isn't (not changing, that is)...
>> c=22;
>> L=4;
>> j=0;
>> for i=1:2:c-(L-1),e=i:i+L-1;
j=j+1;
disp([i e j]),end
1 1 2 3 4 1
3 3 4 5 6 2
5 5 6 7 8 3
7 7 8 9 10 4
9 9 10 11 12 5
11 11 12 13 14 6
13 13 14 15 16 7
15 15 16 17 18 8
17 17 18 19 20 9
19 19 20 21 22 10
>>
dpb
dpb on 8 Jan 2016
"Did you mean filter function?"
Actually, it's filter2 w/o the trailing "d", but on reflection (so to speak), for your patticular application it's probably just as easy to do what you're doing as the edge effects issues will probably be more overhead than the loop given the actual process. Of course, use the simplifications outlined above in the indices calc's.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices 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!