why is my vector empty

조회 수: 6 (최근 30일)
Alex Skantz
Alex Skantz 2021년 10월 23일
댓글: Image Analyst 2021년 10월 24일
hi everyone,
I'm trying to take a code I have that turns sentences into integers and display the sentence in a word array.
The code I have looks like this so far:
N=input('what is N?: ') %establishes N
all_A = {};
A=[];
while true
s = input('give me a sentence!: ','s'); %asks user for sentence input
if strcmpi(strtrim(s), 'end')
break;
end %will end the loop if end is typed
A=[A;string(s)];
end
for i=1:size(A,1)
disp(double(A{i}))
end
row_=(double(A{i}));
col_=N;
wordarray=[row_:col_]
for example, if i use the following inputs:
what is N?: 10
N =
10
give me a sentence!: are you ready kids?
give me a sentence!: aye aye captain!
give me a sentence!: I can't hear you!
give me a sentence!: AYE AYE CAPTAIN!
give me a sentence!: end
97 114 101 32 121 111 117 32 114 101 97 100 121 32 107 105 100 115 63
97 121 101 32 97 121 101 32 99 97 112 116 97 105 110 33
73 32 99 97 110 39 116 32 104 101 97 114 32 121 111 117 33
65 89 69 32 65 89 69 32 67 65 80 84 65 73 78 33
I would want the word array to look something like
'are you rea
dykids? aye
aye captai
n! I cant h
ear you! A
YE AYE C
APTAIN! '
when i run the script I keep getting
" 1×0 empty double row vector."
what am I doing wrong?
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 10월 23일
'are you rea
12345678901
dykids? aye
12345678901
aye captai
1234567890
n! I cant h
12345678901
ear you! A
1234567890
YE AYE C
12345678
APTAIN! '
123456789
So your expected output has lines of length 11, 11, 10, 11, 10, 8, 9 . What is the algorithm to decide what length the line should be?

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

채택된 답변

Image Analyst
Image Analyst 2021년 10월 23일
Maybe you mean this:
N=input('what is N? ') % establishes N
all_A = {};
A=[];
loopCounter = 1;
while true
s = input('give me a sentence!: ','s'); % asks user for sentence input
if strcmpi(strtrim(s), 'end')
break;
end %will end the loop if end is typed
A = [A, s]
loopCounter = loopCounter + 1;
end
% Pad out to a multple of N.
A((loopCounter+1)*N) = ' ';
len = length(A)
m = mod(len, N)
if m ~= 0
A(len+N-m) = ' ';
% length(A)
end
% Reshape A into N columns
wordarray = reshape(A, [], N)'
  댓글 수: 2
Alex Skantz
Alex Skantz 2021년 10월 24일
how can I geet the numbers to also appear before the character vector? I need both.
Image Analyst
Image Analyst 2021년 10월 24일
Try using sprintf() to prepend the number as soon as you get the string from the user:
s = input('give me a sentence!: ','s'); % asks user for sentence input
s = sprintf('%d %s', loopCounter, s);

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

추가 답변 (2개)

Cris LaPierre
Cris LaPierre 2021년 10월 23일
I'm not sure what you want wordarray to be. However, it's empty because you are using the colon operate and, since all ascii code for letters are greater than 10, the result is an empty array.
good = 1:3
good = 1×3
1 2 3
bad = 5:1
bad = 1×0 empty double row vector

Jan
Jan 2021년 10월 23일
for i=1:size(A,1)
disp(double(A{i}))
end
i = size(A,1) now. Then:
row_=(double(A{i}));
Is a vector of the ASCII values of the last sentence:
row_ = [65 89 69 32 65 89 69 32 67 65 80 84 65 73 78 33];
The colon operator in a:b uses te first element of the inputs a and b, if they are arrays.
wordarray = [row_:col_]
% is the same as:
wordarray = [[65 89 69 32 65 89 69 32 67 65 80 84 65 73 78 33]:10]
% which is processed as
wordarray = [65:10]
% which is []
By the way: [] is Matlab's operator for the concatenation. Because a:b is a vector already, [a:b] is a waste of time only.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by