MATLAB Answers

0

How do reshape cell array row to new vectors?

Christos Antonakopoulos さんによって質問されました 2015 年 11 月 26 日
最新アクティビティ Christos Antonakopoulos さんによって コメントされました 2015 年 11 月 26 日
I have a variable Big_RIR_AVG which is 1*2611 cell.
Big_RIR_AVG (1) is 59*1 double
Big_RIR_AVG (2) is 60*1 double
Big_RIR_AVG (3) is 59*1 cell( numbers + Nan values as strings)
and so on. So generally the number of rows can be different and the type also is either double or cell.
I also have a vector count= [483 373 396 879 480]
Its sum is 2611 as the length of my columns of the initial variable. So i want to 5 new vectors (483*1, 373*1,396*1,879*1,480*1). The first vector i want to take the Big_RIR_AVG(1,1:483)and take all the elements and create this big vector. And so on for the other 4 vectors.
Any ideas??

  3 件のコメント

Walter Roberson
2015 年 11 月 26 日
Is count(K) always greater than or equal to length(Big_RIR_AVG{K}) ? You show indexing Big_RIR_AVG(1,1:483) but your description says that Big_RIR_AVG(1) is only 59 long: is the intention to accumulate elements of Big_RIR_AVG until you have reached the 483 count, then continue on for the next 373 elements, and so on?
David Young
2015 年 11 月 26 日
Walter: sum(count) is equal to length(Big_RIR_AVG), so I don't think he wants to accumulate elements to reach the count - rather, count(K) must specify the number of cells that combine to make the K'th output vector. The question is only whether the cells or their contents are concatenated - I think it must be the latter because he already has the answer to the former in his question.
yes it is, that was my intention exactly. Sorry for the misunderstanding but the code in the answer below is exactly what i needed

サインイン to comment.

1 件の回答

David Young
回答者: David Young
2015 年 11 月 26 日
編集済み: David Young
2015 年 11 月 26 日
 採用された回答

First, there's an error to correct. Big_RIR_AVG(1) is not a 59x1 double. It's a scalar cell containing a 59x1 double. This might seem like a detail, but it's important to understand cell array indexing properly if you're using cell arrays. However, Big_RIR_AVG{1} is a 59x1 double. Check the documentation for cell arrays if you don't understand.
I think that you want to concatenate the contents of each section of your cell array. (It isn't entirely clear, because you give the size of the first output vector as 483*1, but as it combines the first 483 input vectors it will be much longer than this.) This involves converting the string representations of numbers to doubles.
First, deal with those awkward cell arrays of strings. Assuming that the cell arrays are all columns and each string represents one number in a sensible format, this should do it:
cellind = cellfun(@iscell, Big_RIR_AVG);
% convert cell vectors to double
BRA_noCells(cellind) = cellfun(@str2double, Big_RIR_AVG(cellind), ...
'UniformOutput', false);
% just copy double vectors
BRA_noCells(~cellind) = Big_RIR_AVG(~cellind);
Now do the concatenation of each section of the array
counts = [483 373 396 879 480];
starts = cumsum([1 counts]); % starting points of segments
combinedVectors = cell(1, length(counts));
for k = 1:length(counts)
combinedVectors{k} = vertcat(BRA_noCells{starts(k):starts(k+1)-1});
end
Then combinedVectors{1} is the concatenation of the first group, and so on.
Of course, you need to check that this works correctly with your data.

  1 件のコメント

Thank you this was exactly what i needed. It works, especially the first lines are really helpful since i do not have much experience in cell handling.

サインイン to comment.



Translated by