Returning Variables as Characters

조회 수: 6 (최근 30일)
Trevor Badji
Trevor Badji 2017년 12월 21일
댓글: Walter Roberson 2017년 12월 26일
I have function such that
a= rand (1,10); % it generates random real numbers from 0 to 1
i=1:10;
if b(i)< 0.4;
b(i)= 'A';
else if
b(i) = 'B'
end
the purpose of this code is for example
b = 0.23 0.45 0.89 0.74 0.48 0.98 0.12 0.05 0.56 0.14
I want it to return as a result
b= A B B B B B A A B A
  댓글 수: 2
Guillaume
Guillaume 2017년 12월 21일
@Dogukan,
We really don't appreciate when the original poster edit their question away once they've got an answer. This is not a private consulting service. The answers we post are not just for you, they're intended to help anybody with the same issue. If you edit the question away, the answers become meaningless.
It also makes us a lot less likely to answer your questions in the future.
Thankfully, MathWorks doesn't like it either and have a policy of restoring questions edited away to their original state, so it's also pointless.
Rena Berman
Rena Berman 2017년 12월 26일
(Answers Dev) Restored edit

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

답변 (2개)

Stephen23
Stephen23 2017년 12월 21일
편집: Stephen23 2017년 12월 21일
method one: character code:
>> b = char(66-(a<0.4))
b = ABBBBBAABA
method two: sprintf:
>> b = sprintf(' %c',66-(a<0.4))
b = A B B B B B A A B A
method three: indexing (char):
>> c = 'AB';
>> b = c(2-(a<0.4))
b = ABBBBBAABA
method four: indexing (cell):
>> c = {'A','B'}65_66
>> b = c(2-(a<0.4))
b = 'A' 'B' 'B' 'B' 'B' 'B' 'A' 'A' 'B' 'A'
method five: generate characters directly using randi:
>> char(randi([65,66],1,10))
ans = ABBBBBAABA

Pawel Jastrzebski
Pawel Jastrzebski 2017년 12월 21일
편집: Pawel Jastrzebski 2017년 12월 21일
clear all;
clc;
data = rand (1,10)
%logical vector to establish when datapoint is 'A' and 'B'
% Condition 1: 'A' if 'data <= 0.5'
% Condition 2: 'B' if 'data > 0.5'
logicValA = data <= 0.5;
logicValB = data > 0.5;
% Use logical vectors to separate 'data' into 'A's' and 'B's':
% 'newData' as 'char vector' → all values concatenated
newData(logicValA) = 'A'
newData(logicValB) = 'B'
% 'newData1' as 'cell array' → all values separated
newData1 = {}
newData1(logicValA) = {'A'}
newData1(logicValB) = {'B'}
  댓글 수: 2
Trevor Badji
Trevor Badji 2017년 12월 21일
편집: Matt J 2017년 12월 21일
clear all;
clc;
N=10;
probVector = [0.4 0.05 0.2 0.15 0.2];
b = rand (1,N)
%logical vector
% 'A' if 'data <= 0.5'
% 'B' if 'data > 0.5'
logicValA = 0<b<=probVector(1);
logicValB = probVector(1)< b <probVector(1)+probVector(2);
logicValC = probVector(1)+ probVector(2)<b<= probVector(1)+probVector(2)+probVector(3);
logicValD = probVector(1)+ probVector(2)+probVector(3)<b<=probVector(1)+probVector(2)+probVector(3)+probVector(4);
logicValE = probVector(1)+probVector(2)+probVector(3)+probVector(4)<b<= probVector(1)+probVector(2)+probVector(3)+probVector(4)+probVector(5);
% 'newData' as 'char vector' → all values concatenated
newData(logicValA) = 'A'
newData(logicValB) = 'B'
newData(logicValC) = 'C'
newData(logicValD) = 'D'
%newData(logicValE) = 'E'
% 'newData1' as 'cell array' → all values separated
newData1 = {}
newData1(logicValA) = {'A'}
newData1(logicValB) = {'B'}
newData1(logicValC) = {'C'}
newData1(logicValD) = {'D'}
newData1(logicValE) = {'E'}
I have changed my code regarted to yours but now it only returns E what is the missing point ?
Walter Roberson
Walter Roberson 2017년 12월 26일
A<B<C means to compare A and B first getting out 0 (false) or 1 (true), and then to compare that 0 or 1 to C. You need to test A<B&B<C

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by