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 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 8 月 23 日
Give an example? what result did you expect?
Rakhi Singh
Rakhi Singh 2018 年 8 月 23 日
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

サインインしてコメントする。

 採用された回答

Jan
Jan 2018 年 8 月 23 日

0 投票

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

2 件のコメント

Rakhi Singh
Rakhi Singh 2018 年 8 月 23 日
Thank you. This works.
Rakhi Singh
Rakhi Singh 2018 年 8 月 23 日
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 についてさらに検索

タグ

質問済み:

2018 年 8 月 23 日

コメント済み:

2018 年 8 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by