来自matlab中矩阵的图像(images from matrix in matlab)

编程入门 行业动态 更新时间:2024-10-24 14:28:02
来自matlab中矩阵的图像(images from matrix in matlab)

我编写了一个matlab代码,它将加载写在文本文件中的矩阵,然后我想将其显示为图像。 文本文件以随机方式包含从0到2的整数,我想用不同的颜色表示每个,例如0表示白色,1表示某种颜色,2表示不同颜色。 我将在下面提供matlab代码:

clc; clear all; for i=1:10 k=num2str(i); f = strcat('test_file_num_',k,'.txt'); a{i} = fileread(f); [m,n] = size(a{i}); a{i} = reshape(a{i},12,10); a{i} = a{i}'; a{i} = a{i}(:,1:10); end

文件夹中有10个文本文件,每个文件包含一个包含整数0到2的随机矩阵,每个文本文件的名称以“test_file_num_”开头,而在{i}中我的矩阵为10x10矩阵。 现在我想将a {i}矩阵表示为图像或图形或任何东西,但要有一些能够以不同颜色显示矩阵内容的东西。 感谢你的帮助。

I have written a matlab code which would load the matrix written in a text file and then I want to show it as an image. The text file contains integers number from 0 to 2 in a random fashion and I want to represent each in a different colors e.g. 0 in white,1 in some color and 2 in a different color. I would provide the matlab code just below :

clc; clear all; for i=1:10 k=num2str(i); f = strcat('test_file_num_',k,'.txt'); a{i} = fileread(f); [m,n] = size(a{i}); a{i} = reshape(a{i},12,10); a{i} = a{i}'; a{i} = a{i}(:,1:10); end

There are 10 text file in the folder each of which contain a random matrix containing integer numbers 0 to 2 and name of each text file starts with "test_file_num_" and in a{i} I have the matrix which is 10x10 matrix. Now I want to represent the a{i} matrix as an image or graph or anything but to have something which would show the contents the matrix in a different color. Thanks for all your help.

最满意答案

如果文件中的数据是结构化的,意味着数字之间有标签或空格,您可以直接使用importdata将数据加载到矩阵中。 之后,您可以使用imagesc生成图像。 要指定颜色,可以使用色彩colormap功能更改颜色colormap 。 所以你的代码看起来像这样:

% Example data A = floor(3*rand(10)); % Change colormap with just 3 colors (Red,Green,Blue) cmap = [1 0 0;0 1 0;0 0 1]; figure; imagesc(A); colormap(cmap); colorbar;

希望有所帮助!

If the data in your file is structured, meaning has tabs or spaces between the numbers you can directly use importdata to load your data into a matrix. After that, you can use imagesc to produce the image. To assign the colors you can change the colormap using the colormap function. So your code would look something like this:

% Example data A = floor(3*rand(10)); % Change colormap with just 3 colors (Red,Green,Blue) cmap = [1 0 0;0 1 0;0 0 1]; figure; imagesc(A); colormap(cmap); colorbar;

Hope that helps!

更多推荐

本文发布于:2023-08-05 19:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   图像   matlab   images   matrix

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!