Hi, is it possible to avoid the loop? Thanks.
x=(1:3:r);
Unrecognized function or variable '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 comentarios

DGM
DGM el 29 de Jul. de 2023
What is z? What is Tr?
What is the value of r before it's redefined? Is it the same?
pipin
pipin el 29 de Jul. de 2023
excuse me.. z=r...i change it in code
Tr is a matrix
pipin
pipin el 29 de Jul. de 2023
[r,c]=size(Tr);
x=(1:3:r);
E1=zeros(r,c);
for i=1:c
E1(x,i)=E(Tr(r,i),i);
end
Image Analyst
Image Analyst el 29 de Jul. de 2023
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
pipin el 29 de Jul. de 2023
thanks your help I made some mistakes while creating the code because it's a bit laborious. Here's the correct version
%input:
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
Tr(x,i)
E(Tr(x,i),i)
E1(x,i)=E(Tr(x,i),i);
end
E1

Iniciar sesión para comentar.

 Respuesta aceptada

Bruno Luong
Bruno Luong el 30 de Jul. de 2023
Editada: Bruno Luong el 30 de Jul. de 2023

1 voto

Just to pull out the right answer (my previous answer applied on a WRONG calculation specified by OP)
% vectorized method
c = size(Tr,2);
m = size(E,1);
x = 1:step:m;
E1 = zeros(size(Tr));
E1(x,:) = E(Tr(x,:) + (0:c-1)*m);

2 comentarios

pipin
pipin el 30 de Jul. de 2023
Editada: pipin el 30 de Jul. de 2023
hello.. why do you say that the previous answer was wrong? on the isequal test it was correct
(what is OP?)
Bruno Luong
Bruno Luong el 30 de Jul. de 2023
Editada: Bruno Luong el 30 de Jul. de 2023
OP is Original Poster, it's you who wrote : "excuse me.. z=r...i change it in code" ... remember?
My answer (not the comments below it) is wrong because the question is wrong.

Iniciar sesión para comentar.

Más respuestas (1)

Bruno Luong
Bruno Luong el 29 de Jul. de 2023
Editada: Bruno Luong el 29 de Jul. de 2023

1 voto

If you want obscure code by avoiding for-loop
E = rand(10,5)
E = 10×5
0.4433 0.5126 0.5907 0.7564 0.9740 0.9908 0.4916 0.9634 0.8613 0.0549 0.9769 0.2085 0.4765 0.2052 0.7497 0.0450 0.8816 0.9243 0.6475 0.1040 0.8076 0.1900 0.4109 0.6581 0.2044 0.4685 0.1936 0.7925 0.7647 0.4082 0.9868 0.9336 0.2478 0.0403 0.7413 0.1741 0.5068 0.1629 0.0827 0.0434 0.1120 0.7868 0.0827 0.4696 0.7350 0.4545 0.5871 0.0055 0.1273 0.2868
Tr = randi(size(E,1),9,size(E,2))
Tr = 9×5
9 10 5 2 4 9 10 9 9 2 1 10 4 6 2 2 8 2 3 4 8 2 7 9 8 5 7 2 4 5 5 5 7 9 5 5 10 10 4 3 9 8 3 3 7
[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
E1 = 9×5
0.1120 0.5068 0.4765 0.2052 0.7413 0 0 0 0 0 0 0 0 0 0 0.1120 0.5068 0.4765 0.2052 0.7413 0 0 0 0 0 0 0 0 0 0 0.1120 0.5068 0.4765 0.2052 0.7413 0 0 0 0 0 0 0 0 0 0
% 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
E1 = 9×5
0.1120 0.5068 0.4765 0.2052 0.7413 0 0 0 0 0 0 0 0 0 0 0.1120 0.5068 0.4765 0.2052 0.7413 0 0 0 0 0 0 0 0 0 0 0.1120 0.5068 0.4765 0.2052 0.7413 0 0 0 0 0 0 0 0 0 0

13 comentarios

Bruno Luong
Bruno Luong el 29 de Jul. de 2023
PS: I have a feeling that is NOT what you expect to get.
pipin
pipin el 29 de Jul. de 2023
thanks your help I made some mistakes while creating the code because it's a bit laborious. Here's the correct version
%input:
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
Tr(x,i)
E(Tr(x,i),i)
E1(x,i)=E(Tr(x,i),i);
end
E1
"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)
E = 10×5
3 10 7 6 1 1 6 1 1 1 8 2 4 3 5 7 9 6 6 5 9 8 1 7 5 4 10 3 6 1 7 9 8 5 3 2 3 8 4 1 10 1 3 0 2 2 5 2 8 2
Tr = randi(size(E,1),row,size(E,2))
Tr = 10×5
3 7 9 10 2 4 3 2 5 8 4 10 9 3 10 10 3 5 7 6 5 6 1 10 7 3 5 4 6 1 2 10 8 5 1 8 1 8 1 2 7 7 10 1 10 1 9 1 7 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
E1 = 10×5
8 9 3 8 1 0 0 0 0 0 0 0 0 0 0 2 2 1 5 1 0 0 0 0 0 0 0 0 0 0 1 5 8 7 1 0 0 0 0 0 0 0 0 0 0 3 1 7 5 1
% vectorized method
E1=zeros(r,c);
i=1:c;
E1(x(:) + (i-1)*r) = E(Tr(x,i) + (i-1)*size(E,1));
E1
E1 = 10×5
8 9 3 8 1 0 0 0 0 0 0 0 0 0 0 2 2 1 5 1 0 0 0 0 0 0 0 0 0 0 1 5 8 7 1 0 0 0 0 0 0 0 0 0 0 3 1 7 5 1
pipin
pipin el 29 de Jul. de 2023
:D
very good
pipin
pipin el 29 de Jul. de 2023
Editada: pipin el 29 de Jul. de 2023
i try it with
row=500000;
columns=500;
step=30;
Elapsed time is 20.148787 seconds. (mYcODE)
Elapsed time is 18.746811 seconds. (VECTORIZED CODE)
Why do you think vectorization doesn't improve execution time?
i use:
Processore Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz 2.71 GHz
RAM installata 8,00 GB (7,88 GB utilizzabile)
Tipo sistema Sistema operativo a 64 bit, processore basato su x64
Bruno Luong
Bruno Luong el 29 de Jul. de 2023
Editada: Bruno Luong el 29 de Jul. de 2023
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
Elapsed time is 0.607406 seconds.
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
Elapsed time is 0.633170 seconds.
pipin
pipin el 29 de Jul. de 2023
Editada: pipin el 29 de Jul. de 2023
20 sec (my method) vs 18 sec (vectorized) are similar
often with vectorization I see advantages of 1:10 as speed compared to loops
likely because this pc has few cores and is not benefited by parallel computing
pipin
pipin el 29 de Jul. de 2023
ok thank
Bruno Luong
Bruno Luong el 29 de Jul. de 2023
Editada: Bruno Luong el 29 de Jul. de 2023
"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.
pipin
pipin el 1 de Ag. de 2023
Editada: pipin el 1 de Ag. de 2023
hi, i'm using your method vectorized
% 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
I have seen that Tr can also assume the value zero which would lead to an error in the function (you cannot search for a zero index)
is it possible to do the search of the indexes excluding if the value is zero?
i solve it:
[r,c]=size(MinTrade_Tab_Short);
a=MinTrade_Tab_Short+ (0:c-1)*r;
a2=a+(~a); %tolgo lo zero (per evitare errore riga sucessiva)
E1 = E(a2);
E2=E1.*(a>0);
it's good for you?
Bruno Luong
Bruno Luong el 2 de Ag. de 2023
Editada: Bruno Luong el 2 de Ag. de 2023
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
Bruno Luong el 2 de Ag. de 2023
Editada: Bruno Luong el 2 de Ag. de 2023
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
pipin el 2 de Ag. de 2023
Correct..thank you!

Iniciar sesión para comentar.

Categorías

Más información sobre General Applications en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Jul. de 2023

Editada:

el 2 de Ag. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by