필터 지우기
필터 지우기

Convert a vector in Character array after a for loop

조회 수: 1 (최근 30일)
luca
luca 2019년 7월 24일
편집: Stephen23 2019년 7월 24일
Hi, given the following code
n=length(x)
f = zeros(1,n)
for i = 1:n
if x(i) == 1
f(1,i) = 22;
elseif x(i)==2
f(1,i) = 23;
elseif x(i)==3
f(1,i)= 21;
elseif x(i)==4
f(1,i) = 23;
elseif x(i)==5
f(1,i)= 24;
end
end
C= char (f)
I don't know why but the 'char' conversion doesn't work giving to me the following
so with nothing inside the vector, that I need later for a switch case.
Do you know how to fix the problem?
  댓글 수: 8
luca
luca 2019년 7월 24일
Probably Char is not the right Command.
What I want to do is. Create a vector f through the for cycle and then use this vector as switch case. The SWITCH expression must be a scalar or a character vector, but in my case f is an array with numeric value. Do you know hot to do?
Stephen23
Stephen23 2019년 7월 24일
편집: Stephen23 2019년 7월 24일
"I don't know hot to do because I'm a really beginner."
That is okay, we are here to help. But so far you have not actually described what you are trying to achieve: you have not explained how the result/output should be derived from the input, or provided example input and output arrays.
Without a specification we have to rely on guessing what you are trying to achieve.
Forget about code, what are you trying to do ?

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

채택된 답변

Stephen23
Stephen23 2019년 7월 24일
Avoiding character manipulations using basic indexing:
>> x = [1,2,3,1];
>> V = 'A':'Z';
>> V(x)
ans = ABCA
  댓글 수: 3
luca
luca 2019년 7월 24일
Sorry may I ask you one more thing?
With basic indexing I have a problem. The fourth value is 1. 1 should be always the case A. But in this case is E. and the code display qwert.
Do you know how to fix in order to have Case A also for the fourth value.
THANKS
Stephen23
Stephen23 2019년 7월 24일
편집: Stephen23 2019년 7월 24일
Instead of
switch V(i)
you need
switch V(f(i))
Otherwise you are just iterating over the elements of V, which is unlikely to be useful.

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

추가 답변 (1개)

Matt J
Matt J 2019년 7월 24일
Perhaps this is what you want?
>> f=[1,2,3,4], F=char(64+f)
f =
1 2 3 4
F =
'ABCD'
  댓글 수: 2
luca
luca 2019년 7월 24일
What I want to do is Create a vector f through the for cycle and then use this vector as switch case. The SWITCH expression must be a scalar or a character vector, but in my case f is an array with numeric value. Do you know hot to do?
Matt J
Matt J 2019년 7월 24일
You can loop over the elements of f,
n=length(x)
f = zeros(1,n)
for i = 1:n
switch f(i)
case 22
case 23
case 24
...
end
end
but I have a nagging feeling that a switch-case is not the best way of doing whatever it is you're trying to do.

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

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by