对于m-by-m(方形)数组,如何将所有行连接成大小为m ^ 2的列向量?
答案 0 :(得分:64)
您可以通过几种不同的方式将矩阵折叠到矢量中,具体取决于您希望矩阵的内容如何填充该矢量。以下是两个示例,一个使用函数reshape
(在第一个transposing矩阵之后),另一个使用colon syntax (:)
:
>> M = [1 2 3; 4 5 6; 7 8 9]; % Sample matrix
>> vector = reshape(M.', [], 1) % Collect the row contents into a column vector
vector =
1
2
3
4
5
6
7
8
9
>> vector = M(:) % Collect the column contents into a column vector
vector =
1
4
7
2
5
8
3
6
9
答案 1 :(得分:0)
将矩阵更改为向量时,非常重要的一点是,如果您使用internal class MainPageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// The data service
private MainDataService mainDataService = new MainDataService();
private List<Book> books = new List<Book>();
public List<Book> Books
{
get
{
return this.books;
}
set
{
this.books = value;
this.OnPropertyChanged();
this.OnPropertyChanged("NavItems");
}
}
public IEnumerable<NavigationViewItemBase> NavItems
{
get
{
return Books.SelectMany(
b => (new List<NavigationViewItemBase> { new NavigationViewItemHeader {
Content = b.Title,
Tag = b.Title
} })
.Concat(
b.Sections.Select(s => new NavigationViewItem
{
Content = s.Title,
Icon = new FontIcon { Glyph = "\uE8B7", FontFamily = new FontFamily("Segoe MDL2 Assets") }
})
)
);
}
}
// @param selectedBookIndex: the index of the book whose first section
// should be selected.
public async Task UpdateBooks(int selectedBookIndex)
{
await mainDataService.PrepareData();
this.Books = mainDataService.Books;
}
…
}
例如:
A(:)
您可以在下图中看到更改的方向。
OnNavigatedTo(NavigationEventArgs e)