I want to divide an array up into sections and add each section to its own column in a table.

29 vues (au cours des 30 derniers jours)
I have an array that is 85x48 and I want to break it up into 6 sections of 85 x 8 and make each of those arrays into one column of a table.
ie. Columns 1-8 of the array will be column 1 of the table, columns 9-16 will be column 2 etc.
The size of my array (85x48) may change and thus the number of sections and their size might also change, so I need to set it up so there is not a set number of columns already in the table and I am just manually assigning the data each time.
I have tried using a for loop that determines the starting and ending column for each section and adds that to a table, but I do not know how to specify which column in the table I want it to be written to, so it only ever writes to the first column and writes over all of the previous data.
Any help would be greatly appreciated
rows = 8; %these numbers will change so the table needs to work for any iteration
columns = 6;
wells = 48;
example = rand(85,48);
n_end = 0:rows:wells; %creating an array to specify where the table should be cut
n_start = n_end +1;
n_end(1) =[];
n_start(end)=[];
n_times = [n_start;n_end];
for i = 1:columns
x = example(:,n_times(1,i):n_times(2,i));
Organized_table = table(x);
end

Réponse acceptée

Walter Roberson
Walter Roberson le 16 Déc 2024 à 23:09
example = rand(85,48);
sections = 6;
T = cell2table(mat2cell(example, ones(1,size(example,1)), sections*ones(1,size(example,2)/sections)))
T = 85x8 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 __________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ ____________________________________________________________________ ____________________________________________________________________ __________________________________________________________________________ __________________________________________________________________________ ___________________________________________________________________ 0.61778 0.83819 0.91492 0.34529 0.62993 0.10536 0.79276 0.037855 0.1787 0.40414 0.73115 0.28931 0.38671 0.30195 0.19799 0.17847 0.052444 0.51214 0.43229 0.68549 0.8827 0.72688 0.57806 0.27219 0.28404 0.23525 0.57858 0.6923 0.25727 0.714 0.10165 0.75734 0.12815 0.51649 0.73737 0.4081 0.45846 0.39319 0.14343 0.61627 0.30955 0.87969 0.45196 0.24964 0.80061 0.65145 0.34711 0.24839 0.49088 0.10583 0.91272 0.82558 0.51613 0.39412 0.56334 0.87142 0.17127 0.16685 0.19765 0.6236 0.072444 0.64685 0.014503 0.79518 0.89055 0.087159 0.95846 0.52378 0.31725 0.45959 0.32478 0.93494 0.19341 0.83072 0.86403 0.078836 0.53729 0.68245 0.12724 0.81473 0.22596 0.60674 0.91323 0.76844 0.98845 0.94683 0.45995 0.68192 0.30401 0.50438 0.75823 0.70048 0.031786 0.82836 0.64654 0.24506 0.0038765 0.72805 0.22112 0.11343 0.61679 0.49281 0.94687 0.40443 0.68707 0.087207 0.88031 0.41355 0.51972 0.32257 0.85402 0.75378 0.13365 0.046396 0.50384 0.77315 0.5238 0.47431 0.9828 0.41365 0.34534 0.79377 0.53301 0.75075 0.016756 0.21084 0.78764 0.80188 0.03441 0.63249 0.95161 0.7706 0.7163 0.45482 0.61965 0.25878 0.76623 0.10702 0.61813 0.90643 0.038491 0.73203 0.1154 0.57971 0.21217 0.63867 0.7088 0.70846 0.8734 0.88431 0.99105 0.64147 0.16137 0.62641 0.34107 0.83354 0.72734 0.94279 0.75923 0.9186 0.5583 0.15653 0.011819 0.91009 0.77764 0.18167 0.31299 0.36316 0.019857 0.88189 0.083109 0.36268 0.047597 0.81766 0.20629 0.86042 0.2425 0.102 0.075312 0.013461 0.52176 0.0064553 0.10047 0.28517 0.60924 0.48347 0.22292 0.29299 0.93654 0.04262 0.77756 0.90829 0.054771 0.0824 0.23038 0.53429 0.3514 0.54219 0.13906 0.51397 0.28414 0.19738 0.074833 0.42684 0.28736 0.53851 0.71574 0.28263 0.55223 0.49744 0.49816 0.89817 0.069814 0.1925 0.55426 0.98221 0.89558 0.75532 0.47253 0.87398 0.19085 0.65728 0.8995 0.8872 0.35097 0.75827 0.29058 0.056329 0.30845 0.50832 0.28973 0.513 0.99284 0.46326 0.64467 0.77866 0.73938 0.6321 0.023519 0.030495 0.49514 0.65681 0.43148 0.6735 0.89579 0.24514 0.28024 0.5645 0.056943 0.84655 0.66636 0.087803 0.44291 0.95833 0.73673 0.13411 0.81063 0.11193 0.050241 0.27785 0.28488 0.12515 0.79589 0.5398 0.58177 0.65302 0.052071 0.21615 0.014317 0.98513 0.36215 0.0021831 0.8802 0.83347 0.94043 0.50979 0.92593 0.50212 0.61487 0.65318 0.79809 0.40305 0.76907 0.13543 0.61586 0.031162 0.79126 0.6556 0.19076 0.72739 0.52538 0.78562 0.59153 0.53908 0.8683 0.91399 0.01333 0.93143 0.090856 0.72717 0.88225 0.69787 0.47012 0.52326 0.019978 0.10942 0.44947 0.67241 0.7825 0.3505 0.78473 0.41631 0.66311 0.13383 0.52069 0.80466 0.06263 0.20451 0.83302 0.1968 0.3573 0.69532 0.46537 0.72968 0.55683 0.25673 0.49712 0.074719 0.94482 0.011768 0.21643 0.73415 0.84113 0.99558 0.30557 0.89161 0.54274 0.042468 0.017764 0.58986 0.91346 0.63083 0.31927 0.42468 0.98888 0.19728 0.63837 0.69241 0.82576 0.49234 0.24948 0.96546 0.26831 0.43473 0.14861 0.24034 0.47547 0.22967 0.28838 0.72016 0.16624 0.71203 0.2471 0.18559 0.4724 0.6609 0.15184 0.35953 0.45004 0.98623 0.51975 0.10804 0.2227 0.45079 0.49549 0.29341 0.41629 0.87678 0.63955 0.33097 0.28438 0.13414 0.1875 0.13316 0.22115 0.44608 0.34292 0.93701 0.73759 0.56754 0.042415 0.83392 0.04837 0.41541 0.53803 0.58984 0.78016 0.6098 0.67032 0.5661 0.64753 0.64982 0.28805 0.18704 0.9421 0.83148 0.059643 0.23278 0.037174 0.88646 0.95447 0.93791 0.5634 0.96672 0.59086 0.25247 0.17516 0.39395 0.47212 0.53079 0.53682 0.02285 0.89679 0.24507 0.82626 0.22322 0.61643 0.1989 0.14653 0.20539 0.1098 0.48894 0.69314 0.62678 0.56201 0.51063 0.16478 0.30058 0.80841 0.40916 0.27964 0.75892 0.96733 0.27439 0.23121 0.032917 0.85235 0.4348 0.9489 0.90845 0.26091 0.46583 0.36023 0.68515 0.85405 0.27072 0.16897 0.44151 0.86352 0.05461 0.16458 0.67817 0.42118 0.52694 0.1474 0.79592 0.32815 0.21443 0.34572 0.30732 0.50063 0.69421 0.94072 0.65782 0.92026 0.62554 0.45347 0.9941 0.15722 0.89405 0.41501 0.95205 0.22968 0.67253 0.46048 0.37822 0.857 0.30721 0.44378 0.0029409 0.51184 0.92168 0.53723 0.17018 0.017173 0.54229 0.46755 0.86249 0.14069 0.39352 0.74447 0.96716 0.58171 0.069591 0.025478 0.37565 0.22932 0.76417 0.61974 0.90019 0.649 0.95754 0.21916 0.16862 0.41772 0.28504 0.88427 0.38747 0.16298 0.27135 0.43409 0.4228 0.29336 0.41388 0.88171 0.84284 0.73006 0.13387 0.017635 0.60297 0.091054 0.051849 0.59017 0.43483 0.91508 0.46715 0.25292 0.99011 0.18234 0.57741 0.67424 0.4463 0.84858 0.87076 0.1461 0.15139 0.4956 0.025818 0.4078 0.55538 0.1351 0.80274 0.73478 0.8878 0.15047 0.53258 0.15481 0.31796 0.082203 0.63046 0.83571 0.60176 0.70311 0.54972 0.28947 0.18897 0.83235 0.69565 0.9097 0.41009 0.41053 0.98692 0.71397 0.91658 0.68993 0.24221 0.034614 0.6481 0.038618 0.33009 0.94053 0.94161 0.81424 0.99525 0.19686 0.8293 0.68719 0.62045 0.70699 0.043785 0.25315 0.92745 0.26664 0.93034 0.71054 0.84215 0.97159 0.51977 0.82369 0.73361 0.31162 0.99881 0.71242 0.16145 0.36235 0.60332 0.92774 0.24242 0.97612 0.51804 0.16984 0.51387 0.0088709 0.20555 0.78483 0.88781 0.092835 0.14242 0.88105 0.77899 0.3764 0.63432 0.058673 0.33931 0.099285 0.38932 0.047617 0.56803 0.25839 0.21613 0.58416 0.10628 0.59499 0.1223 0.86841 0.60157 0.62631 0.93813 0.50928 0.70174 0.056352 0.71807 0.75403 0.51194 0.42953 0.30746 0.81015 0.43581 0.29797 0.65855 0.36764 0.19522 0.52921 0.1914 0.58615 0.074677 0.50593 0.97548 0.75548 0.34635 0.47168 0.54989 0.18679 0.83359 0.84902 0.93009 0.88376 0.012516 0.31113 0.88229 0.66711 0.10211 0.84235 0.056676 0.20472 0.7481 0.22629 0.58946 0.66096 0.99736 0.63936 0.19821 0.72277 0.29104 0.56034 0.41849 0.95179 0.49118 0.34899 0.59951 0.47731 0.54264 0.84263 0.8293 0.40722 0.42948 0.74253 0.039376 0.84162 0.69653 0.56565 0.51428 0.81998 0.55551 0.9568 0.91077 0.59558 0.51436 0.030633 0.72567 0.060048 0.043247 0.48177 0.90603 0.97746 0.51671 0.73364 0.37799 0.63826 0.56168 0.18858 0.43185 0.0055233 0.58449 0.30452 0.85021 0.98743 0.027664 0.35306 0.087233 0.36456 0.58044 0.50967 0.0066454 0.22297 0.89515 0.63523 0.98008 0.94255 0.73936 0.73101 0.1913 0.98927 0.021325 0.59924 0.43823 0.12206 0.96723 0.13315 0.58868 0.0080674 0.29068 0.31882 0.69697 0.66805 0.76288 0.30263 0.19503 0.62175 0.1254 0.41895 0.29952 0.30952 0.91509 0.42496 0.53006 0.2894 0.77366

Plus de réponses (1)

Matt J
Matt J le 16 Déc 2024 à 22:26
Modifié(e) : Matt J le 16 Déc 2024 à 22:28
Using mat2tiles from the File Exchange (download from here),
example = rand(85,48);
xcell=mat2tiles(example,[inf,8]);
Organized_table = table(xcell{:})
>> whos Organized_table
Name Size Bytes Class Attributes
Organized_table 85x6 34703 table

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by