How can I write this fprintf description to include multiple vectors?

7 Ansichten (letzte 30 Tage)
Giovanni Virgen
Giovanni Virgen am 28 Jun. 2018
Beantwortet: OCDER am 28 Jun. 2018
So I have to describe my answers by putting them into a single sentence. I have multiple vectors answers and one dot product. I can't get the vectors to display properly. For example, the sentence will say like "vectors a (insert vector) and b (insert vector) can be used to produce a+b which is (insert vector)...." I just need it to display the vectors properly within a sentence.
a = [10 7 -2]; b = [-4 6 -6]; z = a+b; k = a-b; x = dot(a,b); y = cross (a,b); c = 5*a;
  1 Kommentar
Rik
Rik am 28 Jun. 2018
fprintf doesn't really offer the possibility of giving a formatspec for a vector if you plan on including other elements as well. A common trick is to form the fprintf formatspec with sprintf and a few calls to size (don't forget to escape percent signs in sprintf with a second percent sign).

Melden Sie sich an, um zu kommentieren.

Antworten (1)

OCDER
OCDER am 28 Jun. 2018
mat2str will help you write a matrix as a string.
a = [10 7 -2];
b = [-4 6 -6];
z = a+b;
k = a-b;
x = dot(a,b);
y = cross (a,b);
c = 5*a;
save('tempvar.mat', 'a', 'b', 'z', 'k', 'x', 'y', 'c');
T = load('tempvar.mat');
delete('tempvar.mat');
Var = [fieldnames(T)'; cellfun(@mat2str, struct2cell(T), 'un', 0)'];
fprintf('vectors %s (%s), ', Var{:, 1:end-1});
fprintf('vectors %s (%s).\n', Var{:, end});

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by