How can I vectorize the following operation?
for j =1 :n
AllRankings1(1,a{j}) = tC(j);
end
a = {[4] [2 3 1] [] []}
tC = [1 2 0 0]
I want AllRankings1(1,:) as [2 2 2 1]

2 commentaires

KALYAN ACHARJYA
KALYAN ACHARJYA le 23 Août 2018
Give an example? what result did you expect?
Rakhi Singh
Rakhi Singh le 23 Août 2018
I am getting the answer as expected. I want to increase the speed of my code and Ithis step is taking the most amount of time. I have read that vectorization helps in speeding up the code. That's why looking to vectorize the above piece of code

Connectez-vous pour commenter.

 Réponse acceptée

Jan
Jan le 23 Août 2018
a = {[4] [2 3 1] [] []}
tC = [1 2 0 0]
n = cellfun('prodofsize', a);
aa = cat(2, a{:});
C(aa) = repelem(tC, n)

2 commentaires

Rakhi Singh
Rakhi Singh le 23 Août 2018
Thank you. This works.
Since I am using MATLAB 2014a version, replem doesn't work for me. I am using the following command as a replacement of your third line C(aa)=cell2mat(arrayfun(@(a,r)repmat(a,1,r),tC(:),ra(:),'uni',0));
In reality, my a and tC are matrices and the example that I showed you was only corresponding to one rows of a and tC. COuld you also help me convert the 3 lines of code that you wrote, which works for 1 row, assuming that a and tC are matrices? I have written the matrix version of the first two lines and it seems to work fine. I am not able to convert 3 line into a matrix command and that also I want to do using repmat command. COuld you please help again?
n= cell2mat(cellfun(@(c) cellfun('prodofsize', c), a, 'UniformOutput', false))
cl = cell2mat(cat(2,a{:}));
aa = reshape(cl',[ 4,13])';

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by