필터 지우기
필터 지우기

How do you replace the entry of a matrix with a string?

조회 수: 9 (최근 30일)
Ahmed
Ahmed 2013년 11월 28일
댓글: oshawcole 2017년 12월 6일
I'm a little stuck
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 28일
What is the entry of a matrix?
Ahmed
Ahmed 2013년 11월 28일
It is a number, 0, and I want to change it to a string 'monday' I'm making a calendar

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

채택된 답변

Wayne King
Wayne King 2013년 11월 28일
편집: Wayne King 2013년 11월 28일
You can't technically replace an entry of a matrix with an entire string. You would require a cell array for that. You can convert a matrix into numbers and replace single elements with single characters.
For example:
X = randi([0 6],20,20);
X = num2str(X);
X(X=='0') = 'M';
If you tried to replace with 'Monday', you'd get an assignment mismatch problem because you can't put 6 characters in a placeholder for one character.
You would have to use cell arrays for something like that.

추가 답변 (2개)

Jos (10584)
Jos (10584) 2013년 11월 28일
I suggest to use a cell array and keep the original numerical matrix for finding the elements that have to change:
X1 = randi([0 6],10,10)
X2 = num2cell(X1)
X2(X1 == 1) = {'Tuesday'}

Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 28일
a=[0;1;2;3]
b=num2cell(a)
b{1}='Monday'

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by