How to convert matrix to 1-D array and back to matrix form?

8 ビュー (過去 30 日間)
j john
j john 2015 年 7 月 27 日
編集済み: Stephen23 2015 年 7 月 27 日
I have a matrix of size say, W X H, i have to convert the matrix into a 1-D array and perform some operations(bitxor) and again retrieve back my image in the same dimension ie W X H. How to do it? Thanks in advance.

採用された回答

Stephen23
Stephen23 2015 年 7 月 27 日
編集済み: Stephen23 2015 年 7 月 27 日
Use reshape to do both of these:
>> A = [1,2,3,4;5,6,7,8]
A =
1 2 3 4
5 6 7 8
>> B = reshape(A,1,[]) % columnwise
B =
1 5 2 6 3 7 4 8
>> B = reshape(A.',1,[]) % rowwise
B =
1 2 3 4 5 6 7 8
>> reshape(B,size(A))
ans =
1 3 5 7
2 4 6 8
Note how one can use the transpose operator to select between columnwise and rowwise conversion from the matrix to the vector.
  3 件のコメント
Stephen23
Stephen23 2015 年 7 月 27 日
編集済み: Stephen23 2015 年 7 月 27 日
If the number of elements has changed then you will have to decide yourself how this should be dealt with. You have many choices, such as:
  • define a new size based on the new number of elements
  • removing some values
  • padding
  • subsampling
  • interpolating
  • extrapolating
  • etc
What do you want to do?
j john
j john 2015 年 7 月 27 日
okay... Thanks Stephen for helping me. I think i have to remove some elements to keep the number of elements same.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by