From: bourassa on
I have a 3D array part of which is provided below. I want to find the max value in col #1 and return the associated value in col #3. I can do this sequentially using a loop but at 100,000 arrays it takes a long time. Can this operation be vectorized?

I am using Matlab R14 on Win2K with 2GhZ Athlon and 2 GB RAM.

Any assist/hint would be appreciated.


ans(:,:,1) =

0 7.0000 -0.1210
4.0000 12.0000 0.3114
0 11.0000 0.2144
0 3.0000 -0.6723
0 2.0000 -0.7173
0 14.0000 0.3820
1.0000 10.0000 0.1524
0 8.0000 -0.1236
15.0000 16.0000 1.0352
0 4.0000 -0.3511
0 5.0000 -0.3483
0 9.0000 0.0098
1.0000 15.0000 0.8624
0 1.0000 -0.7133
0 13.0000 0.4283
0 6.0000 -0.3487


ans(:,:,2) =

0 3.0000 -0.5904
14.0000 15.0000 0.9299
1.0000 11.0000 0.3627
0 4.0000 -0.6486
0 6.0000 -0.3445
0 7.0000 -0.3803
3.0000 14.0000 0.6902
0 10.0000 0.2323
9.0000 16.0000 1.0095
3.0000 12.0000 0.3947
0 1.0000 -0.7477
6.0000 13.0000 0.5846
0 9.0000 -0.2182
0 7.0000 -0.3020
0 6.0000 -0.2855
0 2.0000 -0.6864


ans(:,:,3) =

0 4.0000 -0.5724
14.0000 15.0000 0.9248
1.0000 11.0000 0.3625
0 3.0000 -0.6885
0 5.0000 -0.3672
0 6.0000 -0.3781
4.0000 14.0000 0.6640
2.0000 10.0000 0.2652
9.0000 16.0000 1.0079
1.0000 12.0000 0.3513
0 1.0000 -0.7321
5.0000 13.0000 0.5925
0 9.0000 -0.2245
0 8.0000 -0.2799
0 7.0000 -0.2540
0 2.0000 -0.6712
From: Aaron Clarke on
a = [0 7.0000 -0.1210
4.0000 12.0000 0.3114
0 11.0000 0.2144
0 3.0000 -0.6723
0 2.0000 -0.7173
0 14.0000 0.3820
1.0000 10.0000 0.1524
0 8.0000 -0.1236
15.0000 16.0000 1.0352
0 4.0000 -0.3511
0 5.0000 -0.3483
0 9.0000 0.0098
1.0000 15.0000 0.8624
0 1.0000 -0.7133
0 13.0000 0.4283
0 6.0000 -0.3487];
a(:,:,2) = [0 3.0000 -0.5904
14.0000 15.0000 0.9299
1.0000 11.0000 0.3627
0 4.0000 -0.6486
0 6.0000 -0.3445
0 7.0000 -0.3803
3.0000 14.0000 0.6902
0 10.0000 0.2323
9.0000 16.0000 1.0095
3.0000 12.0000 0.3947
0 1.0000 -0.7477
6.0000 13.0000 0.5846
0 9.0000 -0.2182
0 7.0000 -0.3020
0 6.0000 -0.2855
0 2.0000 -0.6864];
a(:,:,3) = [0 4.0000 -0.5724
14.0000 15.0000 0.9248
1.0000 11.0000 0.3625
0 3.0000 -0.6885
0 5.0000 -0.3672
0 6.0000 -0.3781
4.0000 14.0000 0.6640
2.0000 10.0000 0.2652
9.0000 16.0000 1.0079
1.0000 12.0000 0.3513
0 1.0000 -0.7321
5.0000 13.0000 0.5925
0 9.0000 -0.2245
0 8.0000 -0.2799
0 7.0000 -0.2540
0 2.0000 -0.6712];

b = reshape(a,[size(a,1)*size(a,3) size(a,2)]);

% The first element of ColMax is the maximum value from column 1
[ColMax,Ind] = max(b,[],1);

% The element in the third column of b at Ind(1) is the corresponding value from
% column 3
Row3Val = b(Ind(1),3);




bourassa <bourassa-m(a)rmc.ca> wrote in message <1422746271.68841.1278262226572.JavaMail.root(a)gallium.mathforum.org>...
> I have a 3D array part of which is provided below. I want to find the max value in col #1 and return the associated value in col #3. I can do this sequentially using a loop but at 100,000 arrays it takes a long time. Can this operation be vectorized?
>
> I am using Matlab R14 on Win2K with 2GhZ Athlon and 2 GB RAM.
>
> Any assist/hint would be appreciated.
>
>
> ans(:,:,1) =
>
> 0 7.0000 -0.1210
> 4.0000 12.0000 0.3114
> 0 11.0000 0.2144
> 0 3.0000 -0.6723
> 0 2.0000 -0.7173
> 0 14.0000 0.3820
> 1.0000 10.0000 0.1524
> 0 8.0000 -0.1236
> 15.0000 16.0000 1.0352
> 0 4.0000 -0.3511
> 0 5.0000 -0.3483
> 0 9.0000 0.0098
> 1.0000 15.0000 0.8624
> 0 1.0000 -0.7133
> 0 13.0000 0.4283
> 0 6.0000 -0.3487
>
>
> ans(:,:,2) =
>
> 0 3.0000 -0.5904
> 14.0000 15.0000 0.9299
> 1.0000 11.0000 0.3627
> 0 4.0000 -0.6486
> 0 6.0000 -0.3445
> 0 7.0000 -0.3803
> 3.0000 14.0000 0.6902
> 0 10.0000 0.2323
> 9.0000 16.0000 1.0095
> 3.0000 12.0000 0.3947
> 0 1.0000 -0.7477
> 6.0000 13.0000 0.5846
> 0 9.0000 -0.2182
> 0 7.0000 -0.3020
> 0 6.0000 -0.2855
> 0 2.0000 -0.6864
>
>
> ans(:,:,3) =
>
> 0 4.0000 -0.5724
> 14.0000 15.0000 0.9248
> 1.0000 11.0000 0.3625
> 0 3.0000 -0.6885
> 0 5.0000 -0.3672
> 0 6.0000 -0.3781
> 4.0000 14.0000 0.6640
> 2.0000 10.0000 0.2652
> 9.0000 16.0000 1.0079
> 1.0000 12.0000 0.3513
> 0 1.0000 -0.7321
> 5.0000 13.0000 0.5925
> 0 9.0000 -0.2245
> 0 8.0000 -0.2799
> 0 7.0000 -0.2540
> 0 2.0000 -0.6712
From: Mike Bourassa on
Hi Aaron. Thank you for the reply. This gives some insight but isn't quite what I was looking for.

To clarify: I have a 3-D array b of size 16x3x3. For each 16x3 slice, I want to find the max value in Col #1 and return the corresponding value in Col #3. Right now I have a loop that iterates ie. b(:,:,1) then b(:,:,2) then b(:,:,3). For each slice I find the max value, the Col #3 value, and store the cumulative result as vector.

The problem is that the working array is 16x3x500000. It takes a very long time. I was hoping to eliminate the loop by vectorization. But I have yet to come up with a solution.

Again, any assistance/guidance is appreciated.
From: Jan Simon on
Dear Mike,

> Again, any assistance/guidance is appreciated.

Give us a chance to improve your code - please post it.

Kind regards, Jan
From: Mike Bourassa on
Below are first the code, then results and then the test data:

[testNumZeros testIndexZeros] = max(testdecisionZZ(:,1,:)); % Find max col 1

a = []; % Initialize
b = [];

for i = 1:size(testdecisionZZ,3) % Loop through each 'slice'
a = [ a ; testdecisionZZ(testIndexZeros(:,:,i),3,i)];
b = [ b ; testIndexZeros(i) ];
end

Should get:

a =

1.3039
1.2937
1.3048

b =

12
12
12

Test data:

testdecisionZZ

testdecisionZZ(:,:,1) =

0 21.0000 0.7197
0 12.0000 -0.0178
0 10.0000 -0.3366
0 8.0000 -0.2939
0 7.0000 -0.2981
2.0000 17.0000 0.2763
0 3.0000 -0.5141
0 11.0000 -0.1186
0 2.0000 -0.5535
1.0000 19.0000 0.4276
1.0000 5.0000 -0.3463
21.0000 22.0000 1.3039
0 2.0000 -0.5169
0 20.0000 0.8102
0 5.0000 -0.5279
1.0000 18.0000 0.4675
0 14.0000 0.0179
1.0000 15.0000 0.2388
0 6.0000 -0.5043
0 7.0000 -0.4205
1.0000 16.0000 0.2636
0 13.0000 -0.0769


testdecisionZZ(:,:,2) =

0 21.0000 0.7552
0 12.0000 -0.0284
0 10.0000 -0.3379
0 8.0000 -0.2994
0 7.0000 -0.2981
2.0000 17.0000 0.3471
0 4.0000 -0.5106
0 11.0000 -0.1280
0 1.0000 -0.5547
1.0000 19.0000 0.4958
1.0000 5.0000 -0.3533
21.0000 22.0000 1.2937
0 2.0000 -0.5208
0 20.0000 0.6416
0 5.0000 -0.5299
1.0000 18.0000 0.4976
0 14.0000 0.0096
1.0000 16.0000 0.2938
0 6.0000 -0.5063
0 7.0000 -0.4234
1.0000 15.0000 0.2438
0 13.0000 -0.0872


testdecisionZZ(:,:,3) =

0 21.0000 0.7051
0 12.0000 -0.0153
0 10.0000 -0.3350
0 8.0000 -0.2921
0 7.0000 -0.2967
2.0000 17.0000 0.2716
0 3.0000 -0.5134
0 11.0000 -0.1162
0 2.0000 -0.5524
1.0000 19.0000 0.4232
1.0000 5.0000 -0.3445
21.0000 22.0000 1.3048
0 2.0000 -0.5154
0 20.0000 0.8086
0 5.0000 -0.5266
1.0000 18.0000 0.4669
0 14.0000 0.0204
1.0000 15.0000 0.2356
0 6.0000 -0.5030
0 7.0000 -0.4190
1.0000 16.0000 0.2676
0 13.0000 -0.0743