reshaping vector with uneven elements and zero padding

10 visualizaciones (últimos 30 días)
KDRA
KDRA el 31 de Oct. de 2018
Editada: Stephen23 el 31 de Oct. de 2018
Hi!
I have vector with n elements and another variable specifying the length of each row. example, A = [ 5 3 8 2 3 5 8; B = [ 2 3 2 ] I want to create matix where I take first two elements of variable A, new row would be next three elements of variable A and last row would be the last two elements of ariable A. Because they have different dimensions I guess we need to add zeros in the place of missing elements. So the expected result would be: C = [5 3 0; 8 2 3; 5 8 0].
I would appreciate some tips!
K.

Respuestas (2)

Stephen23
Stephen23 el 31 de Oct. de 2018
Editada: Stephen23 el 31 de Oct. de 2018
It would be simpler to use a cell array and mat2cell:
>> A = [5,3,8,2,3,5,8];
>> B = [2,3,2];
>> C = mat2cell(A,1,B);
>> C{:}
ans =
5 3
ans =
8 2 3
ans =
5 8
If you really want one numeric matrix then preallocate an array and use a loop.
  2 comentarios
Stephen23
Stephen23 el 31 de Oct. de 2018
Editada: Stephen23 el 31 de Oct. de 2018
KDRA's "Answer" moved here and formatted properly:
Ok actually it was originally a cell array. This is my code:
for kk = 1:mfiles
for pp = 1:length(S{kk}.Data.RecordedData.Measured)
Data_name{kk}{pp} = S{kk}.Data.RecordedData.Measured{pp}.CurveComment.Text;
Data_side{kk}{pp} = S{kk}.Data.RecordedData.Measured{pp}.EarSide.Text;
for dd = 1:size(S{kk}.Data.RecordedData.Measured{pp}.XYData,2)
Data_measured{kk}{pp}(dd) = str2double(S{kk}.Data.RecordedData.Measured{pp}.XYData{dd}.Y.Text);
end
end
end
Data_name = [Data_name(:)];
Data_side = [Data_side(:)];
Data_description = [Data_name, Data_side];
Data_description = join(Data_description,'_');
The error shows up for the very last line (First argument must be a string array, character vector, or cell array of character vectors). What I want to do is to merge the two multidimentional cell arrays into one and then fit the measured values for each of them. I thought it will be easier using matrices, therefore my question. But if there's easier way to do it, I will be more than happy to learn!
Stephen23
Stephen23 el 31 de Oct. de 2018
Editada: Stephen23 el 31 de Oct. de 2018
@KDRA: you have nested your cell arrays, so you have cell arrays inside other cell arrays:
Data_name{kk}{pp} = ...
I suspect that you tried to un-nest them with these lines, but you used the wrong indexing type, so basically those lines do not change the nesting and the brackets are totally superfluous:
Data_name = [Data_name(:)];
^ ^ convert the cell array to a column.
^ ^ superfluous: there is nothing to concatenate here.
To un-nest you will need to use cell indexing (curly braces):
Data_name = [Data_name{:}];
^ ^ get cell contents in a comma-separated list.
^ ^ concatenate the contents together.
Read more about how comma-separated lists work:

Iniciar sesión para comentar.


madhan ravi
madhan ravi el 31 de Oct. de 2018
Try:
Data_description = string([Data_name, Data_side]);
in your code

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by