Printing cell arrays issue

2 ビュー (過去 30 日間)
Nuno Palma
Nuno Palma 2016 年 12 月 15 日
コメント済み: Nuno Palma 2016 年 12 月 16 日
So, I have two cell arrays. For illustration purpose, let's call them A and B. Cell A and B are both 1x6 cells, let's say that the first elements of A are 1 and 2 and the first element of B is 7. I want to print them so that the value of B stays in the middle of the elements of A, i.e, -> 1 [7] 2. Problem is, when I try to print it, it prints all of the values inside of the cell A and only then the value of B, like 1 [2] 7.
Any ideas?
  3 件のコメント
Nuno Palma
Nuno Palma 2016 年 12 月 16 日
This is part of a more complex problem involving the Dijkstra function. One of the cells contain the paths and the other the distances between them.
I'm already in the end of the project and I'm having issue just showing the results xD
Basically, the cell 'B' will always have one less element than the other one. I just can't seem to print them out in the order I wish, which would be printing one element of A, than one of B, and so on
Nuno Palma
Nuno Palma 2016 年 12 月 16 日
for i = 1:amountofpaths
if dist(i) ~= 0
for j = 1:length(path{i})
fprintf('%d ',path{i}(j));
end
for k = 1:length(dist_parc{i})
fprintf(' (%d) ', dist_parc{i}(k));
end
fprintf('| Total = %d', dist(i));
fprintf('\n');
end
end
end
This is the loop I'm using, and this is the result:
1 2 (7) | Total = 7
1 3 (9) | Total = 9
1 3 4 (9) (11) | Total = 20
1 3 6 5 (9) (2) (9) | Total = 20
1 3 6 (9) (2) | Total = 11
The values in parentheses should be between the other ones. Any ideas?

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2016 年 12 月 16 日
編集済み: Stephen23 2016 年 12 月 16 日
Do not use path as a variable name. As you have already been told, this is the name of a very important inbuilt function path. Use which to check if a name is already in use.
Try this code:
A = {[1,2],[1,3],[1,3,4],[1,3,6,5],[1,3,6]};
B = { 7, 9, [9,11], [9,2,9], [9,2]};
C = {7,9,20,20,11};
for k = 1:numel(C)
tmp = [[NaN,B{k}];A{k}];
fprintf('%d ',tmp(2));
fprintf('(%d) %d ',tmp(3:end))
fprintf('| Total = %d\n', C{k})
end
Which prints this:
1 (7) 2 | Total = 7
1 (9) 3 | Total = 9
1 (9) 3 (11) 4 | Total = 20
1 (9) 3 (2) 6 (9) 5 | Total = 20
1 (9) 3 (2) 6 | Total = 11
  2 件のコメント
Nuno Palma
Nuno Palma 2016 年 12 月 16 日
編集済み: Nuno Palma 2016 年 12 月 16 日
Yes, I forgot to change the variable's name. Wasn't aware it was already built-in matlab.
About the code, I had tried to concatonate them before and I got the same error with this code. 'Error using vertcat Dimensions of matrices being concatenated are not consistent.
I searched it up and it seems all the matrices being concatonated need to have the same number of columns.
I figured out that my 'B' variable has one more element, which is zero, and doesn't even go up for calculations, I think it'd work if I removed it
Nuno Palma
Nuno Palma 2016 年 12 月 16 日
Done. Thanks a lot! :) And for the path input as well !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by