How to extract specific parts of a matrix

In a 512 x 512 matrix where each cell has a number between 0 and 1000, a large part of the cells in this matrix, for example, contains the value 98 (on the left) and on the other side, a large number of cells contain, for example, the value 900.
In the case of when there is only one matrix, it is easy to determine what numbers the matrix contains on both sides.
But in other matrices these numbers are different
I'm looking for a way to automatically specify these numbers on both sides of a matrix
Thank you for your help

 Akzeptierte Antwort

Chad Greene
Chad Greene am 26 Mär. 2021
Bearbeitet: Chad Greene am 28 Mär. 2021
Let's see if I understand what you're trying to do. As I understand it, you have a 512x512 matrix with lots of values of 98 on the left side and lots of 900 on the right side. And you want to automatically find the most common number on the left side of the matrix, and the most common number on the right side? Here's how I'd do that:
M = 1000*rand(512); % some random numbers between 0 and 1000
M(1:10:1e5) = 98; % every tenth value on the left side is 98
M(2e5:12:end) = 900; % every 12th value on the right side is 900
imagesc(M) % displays the matrix
It's difficult to see, because only 1 in 10 values on the left side ar 98, but still 98 is the most common value on the left side. And on the right side, the most common value is 900, because it's every 12th value. Here's how you'd find the most common value on the left and right side:
LeftSide = mode(M(:,1:256),'all')
LeftSide = 98
RightSide = mode(M(:,257:end),'all')
RightSide = 900

3 Kommentare

lech king
lech king am 27 Mär. 2021
Thank you very much for your kindness
Your suggestion is great but there is a problem because the image is a black and white image. In most cases, command mode returns the value 0 as the answer.
In fact, I labeled a CT scan image to extract the lungs on the left and right side of the image.
If I use command mode, I have to specify the exact range, but my CT scan images will also have different dimensions.
Thank you for your guidance in resolving this issue again
Ah, okay. Then I recommend doing the same thing, but only take the mode of the elements that exceed zero. Here's how:
M = 1000*rand(512); % some random numbers between 0 and 1000
M(randi(numel(M),[numel(M)/2 1])) = 0; % set half of the pixels to zero.
M(1:10:1e5) = 98; % every tenth value on the left side is 98
M(2e5:12:end) = 900; % every 12th value on the right side is 900
imagesc(M) % displays the matrix
colormap(gray)
tmp = M(:,1:256); % a temporary matrix of just the left side
LeftSide = mode(tmp(tmp>0),'all') % the mode of left-side values that exceed 0.
LeftSide = 98
tmp = M(:,257:end);
RightSide = mode(tmp(tmp>0),'all')
RightSide = 900
lech king
lech king am 28 Mär. 2021
Thank you very much for your help
It was exactly what I wanted
Excellent

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 26 Mär. 2021

0 Stimmen

Logical indexing would be an easy and fast solution.

3 Kommentare

Chad Greene
Chad Greene am 26 Mär. 2021
Can you explain that?
A = [1, 2, 3; 5 4 4; 6 4 4]; B = (A==4).*A
By this way, you'll get only values which are equal to 4 and the rest is "0".
Good luck.
lech king
lech king am 27 Mär. 2021
Thanks for your help
The problem is that in other matrices it is not clear which number has the highest number
In other words, the main problem is to determine the number that fills the most cells in the matrix

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2020a

Gefragt:

am 26 Mär. 2021

Bearbeitet:

am 28 Mär. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by