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 Kommentare

KALYAN ACHARJYA
KALYAN ACHARJYA am 23 Aug. 2018
Give an example? what result did you expect?
Rakhi Singh
Rakhi Singh am 23 Aug. 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

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 23 Aug. 2018

0 Stimmen

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

2 Kommentare

Rakhi Singh
Rakhi Singh am 23 Aug. 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])';

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 23 Aug. 2018

Kommentiert:

am 23 Aug. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by