MATLAB Answers

How to add space between vector indices in an output

7 views (last 30 days)
Ashlee Rogers
Ashlee Rogers on 26 Sep 2020
Commented: Ashlee Rogers on 26 Sep 2020 at 18:17
I am trying to find out how to make my output have spaces in between the indices of the vector...This is what I have so far, which is giving me the correct vector output:
A=input('Enter values in A in vector form:');
for i=1:length(A)
if (A(i)>0)
B(i) = A(i)^3;
else
B(i) = 0;
end
end
fprintf('The output vector is :\t[')
fprintf('%d',B)
fprintf(']\n')

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Sep 2020
fprintf('%d ',B(1:end-1))
fprintf('%d]\n', B(end))

  3 Comments

Ashlee Rogers
Ashlee Rogers on 26 Sep 2020 at 12:31
My output still looks like [000], and I want my output to look like [0 0 0]. Is this possible?
Walter Roberson
Walter Roberson on 26 Sep 2020 at 13:33
Notice in the first of the two lines I posted, there is a space between the %d and the following '
apostrophe percent d space apostrophe
Ashlee Rogers
Ashlee Rogers on 26 Sep 2020 at 18:17
Yes, thank you! I missed that space! 😊 Very grateful to you

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by