How to add elements to specific rows in existing array without using for-loop?
25 views (last 30 days)
Show older comments
Hello everybody!
I have an existing cell array. For example:
Data =
[0.7482] [0.8258] [0.9619]
[0.4505] [0.5383] [0.0046]
[0.0838] [0.9961] [0.7749]
[0.2290] [0.0782] [0.8173]
[0.9133] [0.4427] [0.8687]
[0.1524] [0.1067] [0.0844]
Into an additional column, I would like to add some other data from another cell array. This additional cell array has two columns. The first one identifies to which row the data belongs, while the seconds one contains the additional data. There is not necessarily additional data to every row. On the other hand there might be also two or more values belonging to one row, which should all end up in one additional cell.
Example of additional data:
Additional_Data =
[5] [10]
[1] [20]
[2] [30]
[2] [40]
Expected outcome:
Complete_Data =
[0.7482] [0.8258] [0.9619] [20]
[0.4505] [0.5383] [0.0046] [1x2 double]
[0.0838] [0.9961] [0.7749] []
[0.2290] [0.0782] [0.8173] []
[0.9133] [0.4427] [0.8687] [10]
[0.1524] [0.1067] [0.0844] []
with the [1x2 double] cell beeing [30 40]
I do not want to use a for-loop. It would also be nice to have an efficient solution, since my original data might have up to 10 000 000 rows, and several data sets will be processed. So run time could be an issue.
Thanks in advance!
--EDIT--
See comment below for clarification.
2 Comments
Matt J
on 17 May 2013
Edited: Matt J
on 17 May 2013
I'm skeptical you're going to find an efficient way to do anything with this data storage scheme. Cell arrays are not made for holding 10000000 rows of anything. They store data non-contiguously in RAM, so data access becomes very slow and inefficient when you get up to array sizes like 10000000.
Also, all MATLAB functions that manipulate cell arrays use for-loops, or something equivalent in performance, internally. There are no options for fast, vectorized analysis of cell arrays.
It might be a good idea to explain the purpose of storing the data this way. For example, why not just use the UNIQUE command to extract groups of data?
Accepted Answer
Matt J
on 17 May 2013
Edited: Matt J
on 17 May 2013
This might be a better option for you than adding a 4th column to Data
j=cell2mat(Additional_Data(:,1));
i=1:length(j);
s=cell2mat(Additional_Data(:,2));
S=sparse(i,j,s);
Now instead of doing
x = Complete_Data(2,4),
you would do
x=nonzeros(S(:,2)),
3 Comments
Matt J
on 18 May 2013
Edited: Matt J
on 18 May 2013
Regarding converting to a struct, it would not be a good idea to have one struct element per row. It would be best to combine all of your original numeric data into a single array and put that in a field of a single struct
Final_Data.parameters=cell2mat(Data);
As for dealing with the case where Additional_Data contains strings, here is a generalization,
j=cell2mat(Additional_Data(:,1));
i=1:length(j);
s=Additional_Data(:,2);
S=sparse(i,j,i);
map=find(any(S,1)),
Final_Data.additional_information=cell(size(Additional_Data,1),1);
Final_Data.additional_information(map)=arrayfun(@(i) s(nonzeros(S(:,i))).',map,'uni',0)
More Answers (1)
Azzi Abdelmalek
on 17 May 2013
Edited: Azzi Abdelmalek
on 17 May 2013
D=num2cell(D);
A_D=num2cell(A_D);
N_D=arrayfun(@(x) A_D(cell2mat(A_D)==x,2),(1:size(D,1))','un',0)
2 Comments
Azzi Abdelmalek
on 17 May 2013
For
D=rand(100000,4);
A_D=[randi(100000,1000,1) rand(1000,1)]
Elapsed time is 89.628606 seconds.
See Also
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!