Could anyone help me with avoiding loops at these situations (if it is possible):
a = [1 2 3 4 5 4 3 2 1 2 3 4 5];
b = [2 3 4 5 4 3 2 1 2 3 4 5 4];
q = 1:13;
c = cell(5,5);
for i = 1:numel(a)
c{a(i), b(i)} = [c{a(i), b(i)}, q(i)];
end
also
for i = 1:numel(a)
c{a(i), b(i)} = unique(c{a(i), b(i)})'
end
For me it is necessary to save every sec! Tnx!

 Akzeptierte Antwort

Stephen23
Stephen23 am 15 Mai 2015
Bearbeitet: Stephen23 am 15 Mai 2015

0 Stimmen

You can use accumarray to get the same output, although the for loop is still about twice as fast as accumarray (for that data). Which method is fastest may depend on the data size.
>> accumarray([a',b'], q, [5,5], @(x){x})
ans =
[] [2x1 double] [] [] []
[8] [] [2x1 double] [] []
[] [ 7] [] [2x1 double] []
[] [] [ 6] [] [2x1 double]
[] [] [] [2x1 double] []

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 15 Mai 2015

Bearbeitet:

am 15 Mai 2015

Community Treasure Hunt

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

Start Hunting!

Translated by