Tabular assignment extremely slow

I am using Matlab 2024a and tabular assignments (tabular.braceAssign as per Profiler) are making any code extremely slow compared to other computers with the same hardware, OS and Matlab version. A factor of 10x slower on my machine for the same piece of code. I tried to reset the cache, check for shadowed functions, but nothing helped.

답변 (1개)

Matt J
Matt J 2025년 6월 12일
편집: Matt J 2025년 10월 7일

0 개 추천

No demo code? Tsk...tsk..
But if I were to guess, it is because you are using brace indexing for row-wise access like in the code below. The overhead of brace indexing a single row is porportional to the size of the entire table, which means it will be slow if you have a large table. It is a known issue which I reported last September. Why you get different speeds on different computers I have no idea, but you should avoid row-wise assignment if you can.
T = combinations(1:100,1:100,1:40,1:40); %multi-row table
t=T(1,:); %one row table
timeit(@()assignTest(t))
ans = 2.3574e-04
timeit(@()assignTest(T))
ans = 0.2678
function assignTest(T)
T{1,:}=[1,1,1,1];
end

댓글 수: 5

Piobaireachd
Piobaireachd 2025년 6월 12일
What is the alternative to row-wise assignment?
Matt J
Matt J 2025년 6월 12일
편집: Matt J 2025년 6월 13일
Convert the table to something else for the purposes of the assignments, e.g, a cell array:
T = combinations(1:100,1:10,1:40); %multi-row table
timeit(@()methodTable(T))
ans = 2.0680
timeit(@()methodCell(T))
ans = 0.1483
function methodTable(T)
for i=1:height(T)
T{i,:}=[1,1,1];
end
end
function methodCell(T)
C=table2cell(T);
for i=1:height(T)
C(i,:)={1,1,1};
end
T=cell2table(C);
end
Peter Perkins
Peter Perkins 2025년 10월 6일
This is unlikely to be the problem, given that the OP said, "A factor of 10x slower on my machine for the same piece of code."
Piobaireachd, with no example, it's impossible to diagnose this. Did you end up solving the problem?
Paul
Paul 2025년 10월 6일
Regrardless of this particular question, is there any progress on improving tabular brace indexing as shown in this thread and this other thread?
Matt J
Matt J 2025년 10월 14일
편집: Matt J 2025년 10월 14일
This is unlikely to be the problem, given that the OP said, "A factor of 10x slower on my machine for the same piece of code."
Since the OP said they are doing row-wise indexing, I think it has to be at least a major contributor to the problem. If I had to guess, I would say some of the different computers being compared are doing a better job of caching the large table2array result that table.braceReference computes. The OP said they are using similar hardware on all machines, but we don't know how similar...

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

질문:

2025년 6월 11일

편집:

2025년 10월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by