How to vectorize?

3 vues (au cours des 30 derniers jours)
pipin
pipin le 29 Juil 2023
Modifié(e) : Bruno Luong le 2 Août 2023
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 commentaires
Image Analyst
Image Analyst le 29 Juil 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 le 29 Juil 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

Connectez-vous pour commenter.

Réponse acceptée

Bruno Luong
Bruno Luong le 30 Juil 2023
Modifié(e) : Bruno Luong le 30 Juil 2023
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 commentaires
pipin
pipin le 30 Juil 2023
Modifié(e) : pipin le 30 Juil 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 le 30 Juil 2023
Modifié(e) : Bruno Luong le 30 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Bruno Luong
Bruno Luong le 29 Juil 2023
Modifié(e) : Bruno Luong le 29 Juil 2023
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 commentaires
Bruno Luong
Bruno Luong le 2 Août 2023
Modifié(e) : Bruno Luong le 2 Août 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 le 2 Août 2023
Correct..thank you!

Connectez-vous pour commenter.

Catégories

En savoir plus sur General Applications dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by