Find number that are above a certain number.

9 visualizaciones (últimos 30 días)
Nathan Formby
Nathan Formby el 21 de Abr. de 2020
Editada: Adam Danz el 23 de Abr. de 2020
I am doing code that will make you enter 8x8 random matrix and the code wants me to:
Count the number of matrix locations in which the cell directly above or directly below have a value greater than 40. Output the matrix row, column and cell value to another matrix called Store when this condition is met. Do not check the boundary terms where this template does not apply.
  5 comentarios
Adam Danz
Adam Danz el 21 de Abr. de 2020
Without asking a question, you're just giving us your assignment to do for you.
Image Analyst
Image Analyst el 21 de Abr. de 2020
Here is the help you need: Homework help

Iniciar sesión para comentar.

Respuesta aceptada

Prasad Reddy
Prasad Reddy el 23 de Abr. de 2020
% I have taken a random matrix M of size 8X8 in the in the place of M you can insert your matrix.
% it took 3 hrs for me complete this code. please give upthumb. if any doubts regarding this
% program please let me know. In this program i have used both forward iterations and backward
% itterations. Latter i added both those matrix and then i created your required sort matrix.
clc
clear all
M=40*(rand(8,8)+0.6)
[r,c]=size(M);
sort_below=zeros(r,c);
sort_above=zeros(r,c);
for i=1:1:(r-1)
for j=1:c
if M(i+1,j) > 40
sort_below(i,j)=M(i,j);
else
sort_below(i,j)=0;
end
end
end
for i=r:-1:2
for j=1:c
if M(i-1,j) > 40
sort_above(i,j)=M(i,j);
else
sort_above(i,j)=0;
end
end
end
for i=1:1:r
for j=1:1:c
if sort_below(i,j)==0
sort_below(i,j)=sort_below(i,j)+sort_above(i,j);
end
end
end
num_elm=sum(sum(sort_below~=0))
p=1;
for i=1:r
for j=1:c
if sort_below(i,j)~=0
sort(p,:)=[i,j,sort_below(i,j)];
p=p+1;
end
end
end
sort
  1 comentario
Adam Danz
Adam Danz el 23 de Abr. de 2020
Editada: Adam Danz el 23 de Abr. de 2020
@Prasad Reddy, this code works except it needs one minor adjustment to skip checking the first and last rows which the OP clarified in a comment under the question.
Here are some suggestions.
  1. Since the task involves checking rows, you can loop through rows instead of individual elements of the matrix.
  2. You can check the 'above' and 'below' values at the same time, no need for separate loops.
  3. Avoid using the variable name "sort" or any other name that matches a common function name. That will avoid shadowing the sort() function and will avoid confusion by users who see sort and exect a function rather than a variable.
Here's just a few lines of code that does the same thing your code does except,
  • it does not check the first and last rows of M
  • it stores the output in a table rather than a matrix
  • the rows of the table are sorted differently than the rows of your output 'sort'
M=40*(rand(8,8)+0.6);
th = 40;
idx = false(size(M));
for i = 2:size(M,1)-1
idx(i,:) = M(i-1,:)>th | M(i+1,:)>th;
end
[rowNum, colNum] = find(idx);
T = table(rowNum, colNum, M(idx), 'VariableNames', {'rowNum', 'colNum', 'Value'});
To compare the T output with your sort output, simply use the same M input and remove the rows of sort that are from the first and last row of M.
% remove 1st & last rows of M
sort(ismember(sort(:,1),[1,8]),:) = [];
% compare 'sort' and 'T' after sorting T to match 'sort' organization
isequal(sort, sortrows(T{:,:}))
% Result:
ans =
logical
1 % outputs match.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by