필터 지우기
필터 지우기

type if x is a square matrix statement

조회 수: 15 (최근 30일)
Zhuoying Lin
Zhuoying Lin 2017년 10월 26일
댓글: michio 2017년 10월 27일
Hi, How do I write the script for "If x is a square matrix funcz will be the 0"?
Thanks!
  댓글 수: 1
Jan
Jan 2017년 10월 27일
A bullet-proof solution is not trivial. Therefore this question is more interesting than it looks like on first glance.

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

답변 (3개)

Stephen23
Stephen23 2017년 10월 26일
편집: Stephen23 2017년 10월 26일
funcz = ~diff(size(x))
  댓글 수: 5
Stephen23
Stephen23 2017년 10월 26일
@Jan Simon: thank you, that is a tidy solution for ND arrays.
Zhuoying Lin
Zhuoying Lin 2017년 10월 26일
Never thought about this question can be such complex. I just start learning Matlab and THANK YOU all for your answers!

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


Jan
Jan 2017년 10월 26일
편집: Jan 2017년 10월 26일
funcz = ~ismatrix(x) || (size(x, 1) ~= size(x, 2));
or with Matlab versions before R2010b:
funcz = (ndims(x) ~= 2) || (size(x, 1) ~= size(x, 2));
This rejects [0 x 1] matrices, but is is questionable, if an empty matrix can be non-square.
To reply funcz=0 for vectors:
funcz = (ndims(x) ~= 2) || all(size(x) ~= 1);
or with modern Matlab versions:
funcz = ~isvector(x)

michio
michio 2017년 10월 26일
[nRow,nCol] = size(x);
if nRow == nCol
funcz = 0;
end
  댓글 수: 3
Jan
Jan 2017년 10월 26일
편집: Jan 2017년 10월 26일
This fails for a [4,2,2] array:
[nR, nC] = size(rand(4, 2, 2))
nR = 4, nC = 4
michio
michio 2017년 10월 27일
Yes, it assumes that the array is 2D. Interesting thread :)

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by