How to resolve this error Error using fprintf Function is not defined for 'cell' inputs.
Ältere Kommentare anzeigen
i want to find out k-shortest path between source and distination. i have creat a matrix for source and distination and for k-shortrst path i have creat a cell arrya in which each cell have three fixed number of path that is shown by hope count (e.g., source distination [hope counts for path1, hope counts for path 2, hope counts for path 3rd]) (e.g., 1 2 [3 4 5]).
n=6;
C1i={[0],[1 2 4],[2 3 4],[1 2 4],[2 3 4],[3 4 5]};
C2i={[1 2 4],[0],[1 3 4],[1 2 3],[2 3 3],[2 3 4]};
C3i={[2 3 4],[1 3 4],[0],[2 3 3],[1 2 3],[1 2 4]};
C4i={[1 2 4],[1 2 3],[2 3 3],[0],[1 3 4],[2 3 4]};
C5i={[2 3 4],[2 3 3],[1 2 3],[1 3 4],[0],[1 2 4]};
C6i={[3 4 5],[2 3 4],[1 2 4],[2 3 4],[1 2 4],[0]};
h = [C1i; C2i; C3i; C4i; C5i; C6i];
for i = 1:n
for j = 1:n
if i ==n && j == n
fprintf('%d %d %s ;\n', i, j, h{i, j});
else
fprintf('%d %d %s\n', i, j, h{i, j});
end
end
which give me output like this..
1 1
Antworten (2)
Bruno Luong
am 7 Sep. 2019
Bearbeitet: Bruno Luong
am 7 Sep. 2019
Not sure the formatting display you want, I simply fix some of issues your code so it can run
n=6;
C1i={[0],[1 2 4],[2 3 4],[1 2 4],[2 3 4],[3 4 5]};
C2i={[1 2 4],[0],[1 3 4],[1 2 3],[2 3 3],[2 3 4]};
C3i={[2 3 4],[1 3 4],[0],[2 3 3],[1 2 3],[1 2 4]};
C4i={[1 2 4],[1 2 3],[2 3 3],[0],[1 3 4],[2 3 4]};
C5i={[2 3 4],[2 3 3],[1 2 3],[1 3 4],[0],[1 2 4]};
C6i={[3 4 5],[2 3 4],[1 2 4],[2 3 4],[1 2 4],[0]};
h = [C1i; C2i; C3i; C4i; C5i; C6i];
for i = 1:n
for j = 1:n
if i ==n && j == n
fprintf('%d %d %s ;\n', i, j, mat2str(h{i, j}));
else
fprintf('%d %d %s\n', i, j, mat2str(h{i, j}));
end
end
end
2 Kommentare
Irfanullah Khan
am 8 Sep. 2019
Bruno Luong
am 8 Sep. 2019
You show the error somewhere else, nothing apparently related to the FPRINTF original question.
How do you expect us to solve it for you?
This is how I'd do it:
hstr = cellfun(@(v) strjoin(compose("%d", v), " "), h); %convert each vector in h into a string
[row, col] = ndgrid(1:size(hstr, 1), 1:size(hstr, 2)); %get all indices of rows and columns
lines = compose('%d %d: %s', row(:), col(:), hstr(:)); %use any formatting you want. I've added a : for clarity
fprintf([strjoin(lines, '\n'), ';\n'])
Note that I'm mixing strings and cell arrays of char vectors on purpose to simplify the code.
2 Kommentare
Irfanullah Khan
am 7 Sep. 2019
Guillaume
am 8 Sep. 2019
I have absolutely no idea what you're asking but it doesn't appear to be related to your original question. The only thing the above does is print your cell array to the command window. It will work with cell arrays of any size as long as the content of the cells is just vectors (of any length).
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!