why is my vector empty

1 view (last 30 days)
Alex Skantz on 23 Oct 2021
Commented: Image Analyst on 24 Oct 2021
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?
Walter Roberson on 23 Oct 2021
'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 on 23 Oct 2021
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)'
Image Analyst on 24 Oct 2021
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);

Cris LaPierre on 23 Oct 2021
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 = 1×0 empty double row vector

Jan on 23 Oct 2021
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.

R2021a

Community Treasure Hunt

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

Start Hunting!