Hi everybody,
I have a matrix(20x400) and I am plotting it with imagesc where y axis having 20 values and xaxis having 400 values.
However, I would like to know how can I scale this xaxis 400 to intervals like between 0:20 = 1, 20:40 = 2 until 380:400 = 20; and setting x axis of imagesc in 0-20 scale with the values of 0-20.
I hope it is clear what I am intended to do.

1 件のコメント

DGM
DGM 2022 年 5 月 4 日
Are you just wanting to set the spacing for the ticks, or are you actually wanting a nonuniform spatial scaling?

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

 採用された回答

Adam Danz
Adam Danz 2022 年 5 月 4 日
編集済み: Adam Danz 2022 年 5 月 4 日

0 投票

Specify x and y values using imagesc(x,y,C)
I think this is what you're looking for
C = rand(20,400); % Replace this with your matrix
x = linspace(1,height(C),width(C));
y = 1:height(C);
imagesc(x,y,C)
axis equal % optional
axis tight % optional
Alternatively, use imresize
figure
I = imresize(C,[20,20]);
imagesc(I)
axis equal
axis tight

6 件のコメント

Ahmet Hakan UYANIK
Ahmet Hakan UYANIK 2022 年 5 月 4 日
Thank you for the answer, it works like that way.
Also if I want to plot Nan values in the data as white color, I have been using pcolor as folllows:
C = replace_matrix; % Replace this with your matrix
x = 1:20;
y = 1:height(C);
imagesc(x,y,C)
[nr,nc] = size(C);
pcolor([C nan(nr,1); nan(1,nc+1)]);
shading flat;
However, it changes the axis again, how can I put white color to NaN values without changing the axis scale?
Ahmet Hakan UYANIK
Ahmet Hakan UYANIK 2022 年 5 月 4 日
okay I did
imagesc(x,y,C,'AlphaData',~isnan(C))
and it works like this.
Ahmet Hakan UYANIK
Ahmet Hakan UYANIK 2022 年 5 月 4 日
One more doubt, when I scale the axis as stated, image is horizantally disturbed(below figure). However, it was not the case before(figure above). Do you know how to handle with it?
Adam Danz
Adam Danz 2022 年 5 月 4 日
My solution scales the x-values 1:400 to 1:20 so that there are still 400 values. This vertically stretches each image pixel because now there are 20 x values for each y value.
If you want the pixels to remain square, you can resize the axes so that the ratio of the width and height correspond the ratio of the data as shown below. But as you see, this results in a short and long axes which is probably not what you're looking for.
C = rand(20,400); % Replace this with your matrix
x = linspace(1,height(C),width(C));
y = 1:height(C);
imagesc(x,y,C)
ax = gca();
ax.Position(4) = ax.Position(3) * height(C)/width(C);
Another approach is to use imresize to rescale your image.
figure
I = imresize(C,[20,20]);
imagesc(I)
Ahmet Hakan UYANIK
Ahmet Hakan UYANIK 2022 年 5 月 4 日
Excellent, thanks!
Adam Danz
Adam Danz 2022 年 5 月 4 日
Glad it worked out. I'll add the imresize suggestion to the main answer for better visibility.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by