Dear all,
I have an image and when it is read in matlab, it is [m*n] matrix, named M,
I need to create another matrix, named MM, which is exactly M, but the indexes are also listed in that.
In fact, MM has 3 columns and the third column is M (:), but the first two columns are i and j which
i=1:m, j=1:n
Thanks in advance

2 件のコメント

Paul Hoffrichter
Paul Hoffrichter 2020 年 12 月 11 日
Trying to follow you. For this example, spell out what you want MM to be.
M =
1 2 3
4 5 6
7 8 9
10 11 12
>> M(:)
ans =
1
4
7
10
2
5
8
11
3
6
9
12
Shabnam M
Shabnam M 2020 年 12 月 11 日
I need also the index related to data of MM

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

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 11 日
編集済み: Ameer Hamza 2020 年 12 月 11 日

0 投票

You can use meshgrid() like this
[r,c] = ndgrid(1:size(M,1), 1:size(M,2));
MM = [r(:), c(:), M(:)];

6 件のコメント

Shabnam M
Shabnam M 2020 年 12 月 11 日
編集済み: Shabnam M 2020 年 12 月 11 日
Thanks for your kind reply,
Actually I checked your proposal but seems sth is wrong and could not correctly create MM, could you recommend another way?
Rik
Rik 2020 年 12 月 11 日
What error did you get? Or is the result not what you wanted? If so, how is it different?
Shabnam M
Shabnam M 2020 年 12 月 11 日
the results are not right, I compared them with raw data and the data are different
Shabnam M
Shabnam M 2020 年 12 月 11 日
The size of r and c are (n*m), but M is (m*n) and that may create the difference?
Rik
Rik 2020 年 12 月 11 日
編集済み: Rik 2020 年 12 月 11 日
The cause of this inconsistency is that Matlab stores arrays in a column-major form. So if you want to use (:) to transform an array into a vector, you need to make sure the result of your coordinate grid matches that order.
The solution is simple: replace meshgrid by ndgrid. Each of these functions has their use, but ndgrid will give the required order in this case. (you could also sort r and c, but that would be an unnecessary operation)
Ameer Hamza
Ameer Hamza 2020 年 12 月 11 日
Yes, ndgrid is more appropriate here. I have edited the answer.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2020 年 12 月 11 日

編集済み:

Rik
2020 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by