Trying to take an input vector or matrix and form new 2x2 matrix from the row,column index for each element of the input vector/matrix where the element is less than the product of the element's row and column, with output in column-major order. Example: Input [1 1;0 4;6 5] would output [2 1; 1 2;3 2].
My code:
function [A] = small_elements(X)
A = [];
[r,c] = size(X);
for xc = 1:c
for xr = 1:r
if X < (xr*xc)
A = [A;[xr;xc]];
end
end
end
end
I was formerly getting a lot of syntax errors. My output has consistently been the empty set. Trying to see why xr;xc is not entering into A.

 채택된 답변

James Tursa
James Tursa 2016년 10월 26일
편집: James Tursa 2016년 10월 26일

1 개 추천

if X(xr,xc) < (xr*xc) % <-- changed X to X(xr,xc)
A = [A;[xr,xc]]; % <-- changed [xr;xc] to [xr,xc]
end

댓글 수: 1

Bryan Moore
Bryan Moore 2016년 10월 26일
Thanks James. I was having significant trouble figuring out the syntax for indexing into a matrix. You are a huge help.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2016년 10월 25일

댓글:

2016년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by