Extract the value according to the number of rows
Show older comments
Hello everyone, I need to extract or reshape the array value (cixd) so that the number of value is based on the num of rows(picture A) and store at the next column. Here shows some illustration. The extraction continues until it goes to 10895 that follows the value of picture A. I tried used reshape but the error says I cannot change the num of the element.

combineResultDisengged=vertcat(FeaturesDis{:});
s{k,:}=size(FeaturesDis{k},1) %extract value rows only
end
[cidxD,cmeansE] = kmeans(combineResultDisengged,500);
R=reshape(cidxD,s{:,1})
end
6 Comments
Adam
on 10 Aug 2017
You are trying to reshape the entire cidxD array every time and since all you seem to want to do is extract segments of different sizes from it you should just extract these into a cell array. You don't need reshape if they will all remain as column matrices.
Siti Suhaila
on 11 Aug 2017
Walter Roberson
on 11 Aug 2017
The second case, the 197 that follows the 185: should that 197 be from the beginning of the vector, or should it be the next 197 elements after the 185 already "used up" ?
Walter Roberson
on 11 Aug 2017
"I need it to correctly display on the next column as well."
That is not possible for a numeric array, as you will have columns of different lengths.
The exception would be if you were to use a "fill value" that got put in to all of the places where there was no value because the column was shorter.
Siti Suhaila
on 11 Aug 2017
Siti Suhaila
on 11 Aug 2017
Answers (1)
Walter Roberson
on 11 Aug 2017
Let the size information be in A. Let the array to be split be B.
total_requested = sum(A);
if total_requested > length(B)
error('A requests more entries than there are in B');
end
B_split = mat2cell(B(1:total_requested), A, 1);
Now B_split is a cell array with column vectors of varying lengths. However you cannot arrange them beside each other until you define how to handle the "holes"
4 Comments
Siti Suhaila
on 11 Aug 2017
Walter Roberson
on 11 Aug 2017
Since you are using consecutive blocks from the second matrix, it is good practice to check first if the total amount of entries that would be requested exceeds the actual size of the second matrix. sum(A) is the total number of entries requested.
Here I do not assume that the 132 lengths in the first matrix add up to exactly the length of the second matrix: I check first that they are not longer than what is available, and then I only use the number requested (which accounts for the possibility that the total is shorter than the number of rows available.)
Siti Suhaila
on 14 Aug 2017
Walter Roberson
on 14 Aug 2017
Are you referring to the fact that M_extract{1,1}(185) is 197, and 197 also happens to be the second length to use according to picture A ?
We do not have your data to test with.
Categories
Find more on Data Type Identification 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!
