Matrix indexing, getting back my original matrix.

2 ビュー (過去 30 日間)
Raldi
Raldi 2014 年 6 月 14 日
コメント済み: Joseph Cheng 2014 年 6 月 14 日
Hi,
I have a matrix
mat= [ 1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9];
and I sub-sample it using the simple command
newMat= mat(1:4:end, 1:4:end);
Is there a way later on to get back the indexes I lost from the original matrix, if I want to reconstruct it in a new matrix likewise
newMat2= zeros(size(mat, 1), size(mat, 2));
newMat2(1:4:end, 1:4:end) = newMat;
So what I want is to replace the columns and rows I lost with lets say NaN.
  1 件のコメント
Jan
Jan 2014 年 6 月 14 日
I do not get the problem. In your example you fill the missing values with zeros. So all you want to do is using nan() instead of zeros()?

サインインしてコメントする。

採用された回答

Joseph Cheng
Joseph Cheng 2014 年 6 月 14 日
Were you looking to get something like this?
mat= [ 1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9];
selCol = 1:4:size(mat,2);
selRow = 1:2:size(mat,1);
newMat= mat(selRow, selCol);
newMat2= NaN*zeros(size(mat));
newMat2(selRow,selCol) = newMat;
newMat2 =
1 NaN NaN NaN 5 NaN NaN NaN 9
NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 NaN NaN NaN 5 NaN NaN NaN 9
NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 NaN NaN NaN 5 NaN NaN NaN 9
NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 NaN NaN NaN 5 NaN NaN NaN 9
NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 NaN NaN NaN 5 NaN NaN NaN 9
  1 件のコメント
Joseph Cheng
Joseph Cheng 2014 年 6 月 14 日
There may be a way to figure out the increments based on the new mat size and original mat but need to think of the conditions.
For instance if you go 1:4:100 you get a 1x25. which you can see 4. but you can just round or floor or ceil +1 for all cases.

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 14 日
You haven't lost anything
mat=repmat(1:9,6,1) % Example
new_mat=mat(1:4,1:4)
You have both the original matrix and the new one, maybe you can give more details about loosing indices
  1 件のコメント
Raldi
Raldi 2014 年 6 月 14 日
No this was just an example to illustrate my problem, assume the matrices are not on the same function and there is no way to access my original matrix. What I actually want is to find a way to select all the rows and columns that I left out in my original matrix and populate them with some new values (for simplicity here NaN)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by