From: Miroslav Pavelka on
I would like to ask you if it is possible to vectorise folowing process:
I have a matrix A and a second matrix of indexes (idx)
idx =

36 55
23 29
30 37
37 45
12 27
48 59
........
I want to perform some calculation on A based on indexes idx and put the
results into matrix B:
B=NaN(size(idx,1),1); % prealocating array
for ii=1:1:length(idx)
B(ii)=max(A(idx(ii,1):idx(ii,2)));
end;
So I want ask, if there is a way to vectorize this calculation.



From: Oleg Komarov on
Miroslav Pavelka <m.pavelka(a)sh.cvut.cz> wrote in message <i10994$htp$1(a)ns.felk.cvut.cz>...
> I would like to ask you if it is possible to vectorise folowing process:
> I have a matrix A and a second matrix of indexes (idx)
> idx =
>
> 36 55
> 23 29
> 30 37
> 37 45
> 12 27
> 48 59
> .......
> I want to perform some calculation on A based on indexes idx and put the
> results into matrix B:
> B=NaN(size(idx,1),1); % prealocating array
> for ii=1:1:length(idx)
> B(ii)=max(A(idx(ii,1):idx(ii,2)));
> end;
> So I want ask, if there is a way to vectorize this calculation.
>
>
B = arrayfun(@(x,y) max(A(x:y)), idx(:,1),idx(:,2));

I think the loop would be faster though.

Oleg
From: Matt Fig on
"Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message
> B = arrayfun(@(x,y) max(A(x:y)), idx(:,1),idx(:,2));
>
> I think the loop would be faster though.
>
> Oleg

Also, to speed up your FOR loop you should pre-allocate with ZEROS instead of NAN. Dealing with nans is slow as has been shown before on this NG.
From: Miroslav Pavelka on
Thank you, this is exactly what I need.
Oleg Komarov wrote:
> Miroslav Pavelka <m.pavelka(a)sh.cvut.cz> wrote in message
> <i10994$htp$1(a)ns.felk.cvut.cz>...
>> I would like to ask you if it is possible to vectorise folowing process:
>> I have a matrix A and a second matrix of indexes (idx)
>> idx =
>>
>> 36 55
>> 23 29
>> 30 37
>> 37 45
>> 12 27
>> 48 59
>> .......
>> I want to perform some calculation on A based on indexes idx and put
>> the results into matrix B:
>> B=NaN(size(idx,1),1); % prealocating array
>> for ii=1:1:length(idx)
>> B(ii)=max(A(idx(ii,1):idx(ii,2)));
>> end;
>> So I want ask, if there is a way to vectorize this calculation.
>>
>>
> B = arrayfun(@(x,y) max(A(x:y)), idx(:,1),idx(:,2));
>
> I think the loop would be faster though.
>
> Oleg
 | 
Pages: 1
Prev: Reassign values
Next: Large fonts and axis labels