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 个评论

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

请先登录,再进行评论。

 采纳的回答

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

2 个评论

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

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by