Vectorize the assignment operation

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 Comments

Give an example? what result did you expect?
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

Sign in to comment.

 Accepted Answer

a = {[4] [2 3 1] [] []}
tC = [1 2 0 0]
n = cellfun('prodofsize', a);
aa = cat(2, a{:});
C(aa) = repelem(tC, n)

2 Comments

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])';

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 23 Aug 2018

Commented:

on 23 Aug 2018

Community Treasure Hunt

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

Start Hunting!