Hello everyone, I have got two matrices(path & path1) and want to put these two together in a way that I get path 3
path =
Columns 1 through 13
14 9 5 2 1 0 0 0 0 0 0 0 0
path1 :
1 2 5 9 14 0 0 0 0 0 0 0 0
1 2 5 9 15 0 0 0 0 0 0 0 0
1 2 5 10 16 0 0 0 0 0 0 0 0
1 3 7 11 17 0 0 0 0 0 0 0 0
1 3 7 12 18 0 0 0 0 0 0 0 0
1 4 8 13 19 0 0 0 0 0 0 0 0
Columns 14 through 19
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Path 3:
14 9 5 2 1 2 5 9 14 0 0 0 0
14 9 5 2 1 2 5 9 15 0 0 0 0
14 9 5 2 1 2 5 10 16 0 0 0 0
14 9 5 2 1 3 7 11 17 0 0 0 0
14 9 5 2 1 13 7 12 18 0 0 0 0
14 9 5 2 1 4 8 13 19 0 0 0 0
Columns 14 through 19
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

 採用された回答

Star Strider
Star Strider 2014 年 5 月 31 日
編集済み: Star Strider 2014 年 5 月 31 日

2 投票

If I understand correctly what you want to do, this will work:
path = path(path>0);
path3 = circshift(path1, [0 size(path,2)-1]);
path3(:,5) = 0;
pathm = [repmat(path, size(path1,1), 1)...
zeros(size(path1,1),size(path1,2)-size(path,2))];
path3 = path3 + pathm
( EDIT: slight reformat to add ‘...’ to make it fit in the window without ambiguity. )

6 件のコメント

Mamali
Mamali 2014 年 6 月 1 日
編集済み: Mamali 2014 年 6 月 1 日
Thanks Star Strider, I get an error in line
pathm = [repmat(path, size(path1,1), 1),zeros(size(path1,1),size(path1,2)-size(path,2))]
'Error using horzcat.
Dimensions of matrices being concatenated are not consistent.'
THERE IS IS A MISTAKE IN LINE ONE AS IT IS NOT RETURNING THE INTENDED MATRIX ,PATH WITH NONZERO VALUES THAT IS...
Also I would really appriciate if you can give me your opinion on the case where * path1 * has got a varying 0 column like below :
path1 :
1 2 5 9 12 14 0 0 0 0 0 0 0
1 2 5 9 15 0 0 0 0 0 0 0 0
1 2 5 10 13 15 16 0 0 0 0 0 0
1 3 7 11 17 0 0 0 0 0 0 0 0
1 3 7 12 18 0 0 0 0 0 0 0 0
1 4 8 13 14 16 17 19 0 0 0 0 0
Columns 14 through 19
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Star Strider
Star Strider 2014 年 6 月 1 日
This is the full code I used:
path = [14 9 5 2 1 0 0 0 0 0 0 0 0];
path1 = [1 2 5 9 14
1 2 5 9 15
1 2 5 10 16
1 3 7 11 17
1 3 7 12 18
1 4 8 13 19];
path1 = [path1 zeros(size(path1,1),19-size(path1,2))]
path = path(path>0);
path3 = circshift(path1, [0 size(path,2)-1]);
path3(:,5) = 0;
pathm = [repmat(path, size(path1,1), 1)...
zeros(size(path1,1),size(path1,2)-size(path,2))];
path3 = path3 + pathm
One thing I did there was to make the matrices consistent. Your path1 matrix had 6 rows in columns 1-13 and 4 rows in columns 14-19. I assume that was an error in cutting-and-pasting from your Command Window, since it would have been represented as a (6x19) matrix in MATLAB. I also assume that in your example path3(5,6) should have been 3 and not 13, so I corrected it.
Adding statements to print path1 and path3 so they will fit here:
fprintf(1,'path1 = \n')
for k1 = 1:size(path1,1)
fprintf(1,[repmat('%4d',1,19) '\n'], path1(k1,:));
end
The output I got for path1 and path3 are:
path1 =
1 2 5 9 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 5 9 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 5 10 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 3 7 11 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 3 7 12 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 4 8 13 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0
path3 =
14 9 5 2 1 2 5 9 14 0 0 0 0 0 0 0 0 0 0
14 9 5 2 1 2 5 9 15 0 0 0 0 0 0 0 0 0 0
14 9 5 2 1 2 5 10 16 0 0 0 0 0 0 0 0 0 0
14 9 5 2 1 3 7 11 17 0 0 0 0 0 0 0 0 0 0
14 9 5 2 1 3 7 12 18 0 0 0 0 0 0 0 0 0 0
14 9 5 2 1 4 8 13 19 0 0 0 0 0 0 0 0 0 0
Which matches what you requested. I have no idea what you are doing, only that I detected a pattern of circular shifting and adding that I then implemented.
I assume that a path1 matrix with varying ‘0’ elements would work in my algorithm the same as this path1 did. Why should it not?
Mamali
Mamali 2014 年 6 月 1 日
編集済み: Mamali 2014 年 6 月 1 日
Thanks Star strider..
It is working fine, Both path and path1 are three dimensional with a cost matrix associated with them( i actually meant the zero column changing in path) .I am trying to merge them in the way you did for every corresponding dimension (Dimension 1 of path with dimension 1 of path1 and so on.. I should first make sure they are of the same dimension). I would appreciate if you can advise me on that.
Star Strider
Star Strider 2014 年 6 月 1 日
My pleasure!
Before I go further, in MATLAB there is no ‘zero column’. All indexing in MATLAB starts with 1. (This will save you agony and error messages.)
To be sure the matrices are the same dimensions, you can:
  1. Either create them that way as I did for your path vector (creating pathm or ‘path matrix’ from it) using repmat and zeros as appropriate. (They are extraordinarily useful functions in this context.) I also used a version of that idea to create the path1 matrix because it was easier than copy-pasting the entire matrix. (I’m lazy.)
  2. Check their sizes by using the size function:
[p1_row,p1_col] = size(path1);
and so with the others, then comparing the row- and column sizes for your matrices-of-interest, and adjusting them as necessary. I strongly disapprove of discarding data, so I suggest zero-padding smaller matrices to match the sizes of larger matrices, unless doing so causes problems with lost rank or high condition numbers. (I don’t know what you are doing, so these are simply general cautions.) You may have to develop specific procedures to address these problems that are appropriate to your applications.
You may need to adjust the call to circshift depending on how you want to create your path3 matrices. Note that I also had to set the 5th column of path3 to zero before adding to get the result you want. I do not know if those will be necessary for your other matrices.
Mamali
Mamali 2014 年 6 月 1 日
I really appreciate your advice, to put it in nutshells ,I am trying to build routes between nodes through a specific node which is 1.
I need to make 3 on the first Iteration , 4 on the second , 5 on the third (meaning the third dimension need to be capped in advance). The routes are calculated once from node 14 to 1 and the other time from 1 to the rest (and the source changes and so on...). I will certainly appreciate your advice as I proceed forward and will post on here.
Star Strider
Star Strider 2014 年 6 月 1 日
This sounds like graph theory, which unfortunately is not an area of my expertise. I will do what I can to help with the MATLAB code, but cannot promise help on the underlying concepts.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2014 年 5 月 31 日

コメント済み:

2014 年 6 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by