import data problem and incorrect answer
Show older comments
hey
i have problem with importing data to matlab
i have 2 groups of cells
A = {
[0.2 0.3 0.4 0.5]
[0 0 0.1 0.2]
[0 0 0.1 0.2]
[0 0 0.1 0.2]
[0 0 0.1 0.2]
[0 0 0.1 0.2]
[0 0 0.1 0.2]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0.8 0.9 1 1]
[0 0 0.1 0.2] }
and B = {
64960.00
380.00
270.00
480.00
360.00
120.00
540.00
253.00
998.00
1352.00
3852.00
2960.00
13562.00
650.00
1100.00 }
when i add it manually and use this loop
D = cell([15 1]);
for ii=1 : numel(A)
D{[ii]} = A{[ii]}().*B{[ii]}();
end
my answer is correct.
but when i add the first data from excel my answer is wrong . in first way i gain cell of D that the value is 15*1
and each cells have 4 items (4 *1)
but in second way i get D with 15*1 cell value but each cells have their own value . for example first one is 1*17 dubble
and second the eight one is 1* 9 !!!!
i dont know how to get my correct answer
7 Comments
Guillaume
on 17 May 2019
If all the vectors are the same size, why are you using cell arrays? Your life would be much easier if you were using matrices:
A = cell2mat(A); %change A into matrix.
B = cell2mat(B);
D = A .* B; %no loop needed!
As for your problem with your excel data, we can't say what you're doing wrong without seeing what you're doing.
If you use double arrays your memory allocation will be lower than using cell arrays.
See below how memory is used with cell and double arrays.
A = [1];
B = {1};
C = [1 2 3 4; 5 6 7 8]; % 2x4 double array
D = {[1 2 3 4];[5 6 7 8]}; % 2x1 cell array includes 1x4 double arrays
Memory usages are below. As it seen in the Bytes column cell arrays always allocates more memory.

Guillaume
on 17 May 2019
Yes, a Luna shows cell arrays will always occupy more memory than the equivalent matrix. Your cell array of scalar B for example will always use 15 times as much memory as the equivalent column vector.
Again, with the example you show, there is no benefit in using cell arrays. More memory, more complicated code, harder to see the data.
As for your question, you still haven't shown us what you're doing so we can't tell what is wrong.
Guillaume
on 17 May 2019
I've already shown you how to use matrices instead of cell arrays.
PC = cell2mat(PC); %convert cell array to matrix
BAC = cell2mat(BAC); %convert cell array to matrix
EV = PC .* BAC; %assuming you're using R2016b or later
Of course, it'd be better if PC and BAC were matrices to start with.
Not sure what you're trying to do with SigmaEV, the code you've posted makes no sense, the loops don't do anything useful. As is, it is equivalent to:
sigmaEV = EV(end, :) + EV(1, :);
Probably no what you want.
Mo Das
on 17 May 2019
Answers (0)
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!