Find number that are above a certain number.
조회 수: 9 (최근 30일)
이전 댓글 표시
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
Adam Danz
2020년 4월 21일
Without asking a question, you're just giving us your assignment to do for you.
채택된 답변
Prasad Reddy
2020년 4월 23일
% 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
Adam Danz
2020년 4월 23일
편집: Adam Danz
2020년 4월 23일
@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.
- Since the task involves checking rows, you can loop through rows instead of individual elements of the matrix.
- You can check the 'above' and 'below' values at the same time, no need for separate loops.
- 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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!