필터 지우기
필터 지우기

plz give me a solution

조회 수: 4 (최근 30일)
Darshan Kanungo
Darshan Kanungo 2015년 5월 30일
댓글: grooshka 2018년 4월 16일
Write a function called large_elements that takes as input an array named X that is a matrix or a vector. The function identifies those elements of X that are greater than the sum of their two indexes. For example, if the element X(2,3) is 6, then that element would be identified because 6 is greater than 2 + 3. The output of the function gives the indexes of such elements found in row-­‐ major order. It is a matrix with exactly two columns. The first column contains the row indexes, while the second column contains the corresponding column indexes. For example, the statement indexes = large_elements([1 4; 5 2; 6 0], will make indexes equal to [1 2; 2 1; 3 1]. If no such element exists, the function returns an empty array.
function found = large_elements(A)
[row col] = size(A);
found = [];
for ii = 1:row
for jj = 1:col
if A(ii,jj) > ii + jj
found = [found; ii jj];
else found = [];
end
end
end
end
Problem 4 (large_elements): Feedback: Your function performed correctly for argument(s) 1
Feedback: Your function performed correctly for argument(s) [1 2 3 4 5 6 7 8 9 10]
Feedback: Your function made an error for argument(s) [10 9 8 7 6 5 4 3 2 1]
Your solution is _not_ correct.
  댓글 수: 4
Walter Roberson
Walter Roberson 2017년 7월 5일
Jyoti, that is what I suggested two years ago.
grooshka
grooshka 2018년 4월 16일
What is the purpose of n variable?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2015년 5월 30일
In the "else" case, leave found unchanged instead of overwriting all of it with [].
  댓글 수: 2
Edward Kofi Mahama
Edward Kofi Mahama 2017년 11월 11일
Please why did you add " n=0" and "n = n+1" to the code. I don't understand why the code should not work without that. Can you please help explain? Thanks in advance
Walter Roberson
Walter Roberson 2017년 11월 11일
Jyoti Sharma added the counter. I do not know why.
There is another way of writing the code that does use a counter:
function y = small_elements(v)
[r,c] = size(v);
n=0;
y = [];
for j = 1:c
for i = 1:r
if v(i,j) < i*j;
n = n+1;
y(n,:) = [i j];
end
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jorge Alberto Fuentes Casillas
Jorge Alberto Fuentes Casillas 2017년 4월 18일
편집: Walter Roberson 2017년 4월 18일
This code is for this case: "Write a function called small_elements that takes as input an array named X that is a matrix or a vector...."
function indexes = small_elements(v)
[i j] = size(v);
position = 0;
indexes_rows = [];
indexes_columns = [];
if ~isempty(v)
for columns = 1:j
for rows = 1:i
elem_in_i = v(rows,columns);
product_ij = rows*columns;
if elem_in_i<product_ij
position = position+1;
indexes_rows(position) = (rows);
indexes_columns(position) = (columns);
end
end
end
if isempty(indexes_rows) || isempty(indexes_columns)
indexes = [];
else
indexes = [indexes_rows',indexes_columns']; % A matrix of only 2 columns is created.
end
else
indexes = [];
end
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by