From: Yi Cao on 17 Jul 2010 12:52 One of many solutions is as follows: [testNumZeros testIndexZeros] = max(testdecisionZZ(:,1,:)); % Find max col 1 a = diag(squeeze(testdecisionZZ(testIndexZeros,3,:))); b = squeeze(testIndexZeros); HTH Yi "Mike Bourassa" <bourassa-m(a)rmc.ca> wrote in message <i1slf3$5c1$1(a)fred.mathworks.com>... > 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
From: Jan Simon on 17 Jul 2010 13:00 Dear Mike, > [testNumZeros testIndexZeros] = max(testdecisionZZ(:,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 Ok. This shows a main problem of your code: Initializing a and b to the empty matrix and letting them grow in each step is horribly slow. It is recommended (again and again in this newsgroup), to pre-allocate the output array: n = size(testdecisionZZ,3); a = zeros(1, n); for i = 1:n % Loop through each 'slice' a(i) = testdecisionZZ(testIndexZeros(:,:,i),3,i); end And as far as I can see, b = testIndexZeros; can be determined outside the loop. Anyhow, do you want to calculate this (I currently have no access to Matlab on my internet computer): n = size(ZZ, 3); [Num, Index] = max(ZZ(:,1,:)) a = ZZ(sub2ind(size(ZZ), Index(:), [3;3;3], transpose(1:n)); Kind regards, Jan
From: Mike Bourassa on 17 Jul 2010 13:52
Very many thanks to all who replied. I learned a lot! Cheers. |