import data problem and incorrect answer

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

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.
Mo Das
Mo Das on 17 May 2019
Edited: Mo Das on 17 May 2019
i need arrays because i must use them a lot later and my data become bigger if i use mat
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.
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.
yeah you are right . but plz look at my example. i dont know how to get it by matrix.
I have alot of data and i must calculate them by their furmulas but i dont know how to solve it by matrix. i try to solve it by cells.
the small example of my data is like below:
PC = { [ 0.5 0.6 0.7 0.8] ; [ 0.5 0.6 0.7 0.8]; [ 0.8 0.9 1 1 ]; [ 0 0 0.1 0.2] }
BAC = { 250 ;150 ;100; 200}
EV = PC * BAC = { [125 150 175 200];[75 90 105 120];[80 90 100 100];[0 0 20 40]
}
i get EV by this in matlab :
EV = cell([numel(PC) 1]);
for ii=1 : numel(PC)
EV{[ii]} = BAC{[ii]}().*PC{[ii]}();
end
SigmaEV = { [ 280 330 400 460]} ( is sum of each items of EV )
i write this for SigmaEV:
SigmaEV=[];
for ii=1 : numel(EV)
for i = 1
SigmaEV{[i]} = EV{[ii]}()+EV{[i]}();
end
end
but it gives me some times correct and sometimes wrong answer! i know it must be my problem but i dont know what is it ?
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.
thank u so much

Sign in to comment.

Answers (0)

Categories

Asked:

on 17 May 2019

Commented:

on 17 May 2019

Community Treasure Hunt

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

Start Hunting!