poster
2019-12-14, 20:13
假设A是一个200项的单元格数组,其中包含4个不同的字符串(每个字符串都有50个重复)。 B是一个带有一些整数的200项向量。
我使用[cellNos cellStartInd enumCells ] = unique(A)并获得这项A等于唯一的字符串中的一个(enumCells是包含排序的枚举的字符串整数1-4,阵列)。
我想使用此信息从B创建一个4x50的值矩阵,以便每一列都具有特定唯一字符串的值。换句话说,我想将B重塑成一个矩阵,其中矩阵根据A每个唯一字符串排列。
回答:
假设您已经知道将要重复多少次,并且所有字符串都以相同的频率重复,您可以执行以下操作:
%# sort to find where the entries occur (remember: sort does stable sorting) [~,sortIdx] = sort(enumCells); %# preassign the output to 50-by-4 for easy linear indexing newB = zeros(50,4); %# fill in values from B: first the 50 ones, then the 50 2's etc newB(:) = B(sortIdx); %# transpose to get a 4-by-50 array newB = newB'; 或者,以更紧凑的方式(感谢@Rich C)
[~,sortIdx] = sort(enumCells); newB = reshape(B(sortIdx),50,4)';
更多&回答... (https://stackoverflow.com/questions/4864628)
我使用[cellNos cellStartInd enumCells ] = unique(A)并获得这项A等于唯一的字符串中的一个(enumCells是包含排序的枚举的字符串整数1-4,阵列)。
我想使用此信息从B创建一个4x50的值矩阵,以便每一列都具有特定唯一字符串的值。换句话说,我想将B重塑成一个矩阵,其中矩阵根据A每个唯一字符串排列。
回答:
假设您已经知道将要重复多少次,并且所有字符串都以相同的频率重复,您可以执行以下操作:
%# sort to find where the entries occur (remember: sort does stable sorting) [~,sortIdx] = sort(enumCells); %# preassign the output to 50-by-4 for easy linear indexing newB = zeros(50,4); %# fill in values from B: first the 50 ones, then the 50 2's etc newB(:) = B(sortIdx); %# transpose to get a 4-by-50 array newB = newB'; 或者,以更紧凑的方式(感谢@Rich C)
[~,sortIdx] = sort(enumCells); newB = reshape(B(sortIdx),50,4)';
更多&回答... (https://stackoverflow.com/questions/4864628)