How to vectorize?
이전 댓글 표시
Hi, is it possible to avoid the loop? Thanks.
x=(1:3:r);
[r,c]=size(Tr);
E1=zeros(r,c);
for i=1:c
E1(x,i)=E(Tr(r,i),i);
end
>> size(Tr)
ans =
21 6
댓글 수: 5
DGM
2023년 7월 29일
What is z? What is Tr?
What is the value of r before it's redefined? Is it the same?
pipin
2023년 7월 29일
pipin
2023년 7월 29일
Image Analyst
2023년 7월 29일
Not seeing it. Again, what is Tr and E (not E1, but E)?
Does your for loop even work? What is the desired output?
If you have any more questions, then attach your data and missing code to read it in after you read this:
pipin
2023년 7월 29일
채택된 답변
추가 답변 (1개)
If you want obscure code by avoiding for-loop
E = rand(10,5)
Tr = randi(size(E,1),9,size(E,2))
[r,c]=size(Tr);
x=(1:3:r);
[r,c]=size(Tr);
x=(1:3:r);
% Your method
E1=zeros(r,c);
for i=1:c
E1(x,i)=E(Tr(r,i),i);
end
E1
% vectorized method
E1=zeros(r,c);
i=1:c;
E1(x(:) + (i-1)*r) = repmat(E(Tr(r,i) + (i-1)*size(E,1)),length(x),1);
E1
댓글 수: 13
Bruno Luong
2023년 7월 29일
PS: I have a feeling that is NOT what you expect to get.
pipin
2023년 7월 29일
"I made some mistakes while creating the code because it's a bit laborious. "
I knew it, not it is not because laborious, it's because you are simply not serious on what you are doing.
row=10;
columns=5;
step=3;
%%*************
E = round(rand(row,columns)*10)
Tr = randi(size(E,1),row,size(E,2))
[r,c]=size(Tr);
x=(1:step:r);
% Your method
E1=zeros(r,c);
for i=1:c
E1(x,i)=E(Tr(x,i),i);
end
E1
% vectorized method
E1=zeros(r,c);
i=1:c;
E1(x(:) + (i-1)*r) = E(Tr(x,i) + (i-1)*size(E,1));
E1
pipin
2023년 7월 29일
Why do you think it does?
This is the timining of the Online server, your PC does not have anough memory IMO. Each array E, E1, and Tr take 2 Gb, so a total of 6 Gb of free RAM is needed.
row=500000;
columns=500;
step=300;
%%*************
E = round(rand(row,columns)*10);
Tr = randi(size(E,1),row,size(E,2));
tic
[r,c]=size(Tr);
x=(1:step:r);
% Your method
E1=zeros(r,c);
for i=1:c
E1(x,i)=E(Tr(x,i),i);
end
toc
tic
% vectorized method II
[r,c]=size(Tr);
x=(1:step:size(E,1));
E1=zeros(r,c);
E1(x,:) = E(Tr(x,:) + (0:c-1)*size(E,1));
toc
pipin
2023년 7월 29일
Bruno Luong
2023년 7월 29일
편집: Bruno Luong
2023년 7월 29일
"often with vectorization I see advantages of 1:10 as speed compared to loops"
Your information is surely NOT up-to-date and too generic to have any real value.
Bruno Luong
2023년 8월 2일
편집: Bruno Luong
2023년 8월 2일
No your solution is NOT good as it only catches Tr == 0 at column 0. For other column a2 is not 0 so you won't correct the overflowed row indexing.
Bruno Luong
2023년 8월 2일
편집: Bruno Luong
2023년 8월 2일
Code to handle the case Tr == 0
c = size(Tr,2);
m = size(E,1);
x = 1:step:m;
E1 = zeros(size(Tr));
Trx = Tr(x,:);
iE = Trx + m*(0:c-1);
tmp = E(max(iE,1));
tmp(Trx == 0) = 0;
E1(x,:) = tmp;
it does not handle still the case Tr > m or Tr < 0.
If you havve data that are not valid for proper indexing it will be a mess to deal with.
pipin
2023년 8월 2일
카테고리
도움말 센터 및 File Exchange에서 General Applications에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!