How to reshape a cell array ?

Hi , I have a this cells :
I want to reshape them , I want each cell include a 95*1 cells which all 95 data are the same value we have for the cell before reshaping. for example look at row 27 . the value is 5.9410 . I want it to be a 95*1 cell which all of the rows has the value of 5.9410. (pls forget about row 28 and assume that in each row we have a single value)

3 Comments

This is a little confusing. Can you give a very small example of the input and expected output?
the input is 672*1 cell array , which in each row we have a single value . I want it to be 672*1 cell array which in each row we have 95*1 cell array and all 95 data has a same data just like before reshaping.
for example if I have a cell array called X and X{1,1}= 2 .I want it to become X{1,1}= {95*1 cell} . and each of 95 cells has the value of 2 .
The two answers below should achieve this.

Sign in to comment.

 Accepted Answer

Assuming the data stored in ESS_estimated_schedule is a cell array,
cellfun(@(x){repmat(x,95,1)},ESS_estimated_schedule)

13 Comments

actually now I checked and saw my input is not a cell array it's a Mat. but I want to become cell with the properties I've said
Then you must make one small change - use num2cell()
cellfun(@(x){repmat(x,95,1)},num2cell(ESS_estimated_schedule))
Stephen23
Stephen23 on 24 Sep 2018
Edited: Stephen23 on 24 Sep 2018
Simpler would be to use arrayfun, then you don't need num2cell.
Stephen's suggestion:
arrayfun(@(x){repmat(x,95,1)},ESS_estimated_schedule)
another question : look this is CT :
and this is Current_week_actual_load_data_reshape :
but when I do this : CT - Current_week_actual_load_data_reshape the answer is like this :
why ? I want the answer to be a 672*1 cell array which each cell contain a 95*1 cell array just like CT and Current_week_actual_load_data_reshape
If CT and Curren_week are cell arrays, then you shouldn't be able to merely subtract them, CR - Current_week. You should get an error
Undefined operator '-' for input arguments of type 'cell'.
Baran Mehrdad
Baran Mehrdad on 24 Sep 2018
Edited: Baran Mehrdad on 24 Sep 2018
no actually I use a "for" loop and for I=1:672 , I gave CT and Current_week_actual_load_data_reshape as a input to the function I called inside "for" loop(like this : Current_week_actual_load_data_reshape{I}(1) , CT{I}(1)) and in that function I do : CT - Current_week_actual_load_data_reshape
Sorry, I don't follow. Can you post a snippet of code, formatted, so I can understand better what you're doing?

I have this "For" loop , and in this loop , I use Schedule_index= 1:length(current_week_time_stamps) which length(current_week_time_stamps)=672 . and then I call CT and DCT and Actual_load_data_reshape in Obtain_current_SoC function .

 for Schedule_index=1:length(current_week_time_stamps)
     Schedule_index
load_prediction_kw= Current_week_predictions{Schedule_index};
[opt_prmt]=update_optimization_paramters(opt_prmt,load_prediction_kw,battery_paramaters, Current_week_actual_load_data);
ESS_estimated_schedule{Schedule_index}=zeros(95,1);
CT{Schedule_index}=zeros(95,1);
DCT{Schedule_index}=zeros(95,1);
[ESS_estimated_schedule{Schedule_index}, Estimated_SoC{Schedule_index},CT{Schedule_index},DCT{Schedule_index},K{Schedule_index},H{Schedule_index},Answer_all]=obtain_ESS_schedule(load_prediction_kw, battery_paramaters, opt_prmt);
[battery_paramaters,ESS_estimated_schedule{Schedule_index}]=obtain_current_SoC(battery_paramaters,opt_prmt(1),CT{Schedule_index}(1),DCT{Schedule_index}(1),Current_week_actual_load_data_reshape{Schedule_index}(1));
 [Current_week_actual_load_data]= update_net_load(Schedule_index,Current_week_actual_load_data,ESS_estimated_schedule);
    end

and this is Obtain_current_SoC :

function  [battery_paramaters,ESS_estimated_schedule]=obtain_current_SoC(battery_paramaters,time_slot_duration,CT,DCT,Current_week_actual_load_data_reshape)
current_SOC=battery_paramaters(6);
if Current_week_actual_load_data_reshape < CT 
  ESS_schedule = CT - Current_week_actual_load_data_reshape ;
  current_SOC=current_SOC+battery_paramaters(3)*time_slot_duration*ESS_schedule ;
else
  ESS_schedule = Current_week_actual_load_data_reshape - DCT ;
  current_SOC=current_SOC+ (1/battery_paramaters(3))*time_slot_duration*ESS_schedule ;
end
battery_paramaters(6)=current_SOC;
ESS_estimated_schedule=ESS_schedule ;
end

where CT - Actual_load_data_reshape and Actual_load_data_reshape - DCT happens.

In your for-loop you define
CT{Schedule_index}=zeros(95,1);
DCT{Schedule_index}=zeros(95,1);
and then you immediately overwrite it via the obtain_ESS_schedule() outputs. Why fill CT{Schedule_index} with zeros?
In any case, if I understand your problem correctly, you expect ESS_schedule to be a vector the same length at CT and Current_week_actual_load_data_reshape (that variable name is way too long). The first thing to check during debugging is whether or not that is true.
The second thing to check is the size of all variables multiplied by ESS_schedule. If you are multiplying two vectors and expect the output to be a vector of the same length containing a piecewise product of the other vectors, they must be the same shape. Here's an example.
% row * column
[1 2 3] * [1;2;3]
ans =
14
% row * row (note the dot!)
[1 2 3] .* [1,2,3]
ans =
1 4 9
Sorry for taking to long. Actually I did not multiplied any variable by ESS_schedule , I just subtract CT from ESS_schedule and I expect to see ESS_schedule to be a 672*1 cell array which each of it's cells contain 95*1 cells . but as you can see it's just 672*1 cell array and each cell contains only one value.
I think I see the cause of your problem now. It's in this line of your code:
[battery_paramaters,ESS_estimated_schedule{Schedule_index}]=obtain_current_SoC(battery_paramaters,opt_prmt(1),CT{Schedule_index}(1),DCT{Schedule_index}(1),Current_week_actual_load_data_reshape{Schedule_index}(1));
I believe CT{Schedule_index} and Current_week_actual_load_data_reshape{Schedule_index} are vectors and in the line above you are only passing the first element of those vector into obtain_current_SoC(). Remove (1) from your inputs if you want to pass the full vectors into that function.

Sign in to comment.

More Answers (1)

Adam
Adam on 24 Sep 2018
Edited: Adam on 24 Sep 2018
cellfun( @(x) x + zeros( 95, 1 ), ESS_estimated_schedule, 'UniformOutput', false );
It seems like rather large replication of redundant data, but that line should do it for you.

Community Treasure Hunt

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

Start Hunting!