Info

此问题已关闭。 请重新打开它进行编辑或回答。

Need to print Matrix Spiral of an image

1 次查看(过去 30 天)
Nimit Jain
Nimit Jain 2016-7-6
关闭: John D'Errico 2016-7-6
Hello, I have written a program in Java to spiral print of 2-dimensional array. Now need that in matlab. My image size is (768, 738, 3)
package ArrayAndString;
public class MatrixSpiral {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] a = {{1,2,3,4,5,6},
{7,8,9,10,11,12},
{13,14,15,16,17,18}};
System.out.println(a.length+" "+a[0].length);
int row = a.length;
int col = a[0].length;
spiralPath(row, col, a);
}
private static void spiralPath(int m, int n, int[][] a) {
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/
while(k<m && l<n)
{
// Print the first row from the remaining rows
for(i=l; i<n;++i)
{
System.out.print(a[k][i]+" ");
}
k++;
/* Print the last column from the remaining columns */
for(i = k;i<m;++i)
{
System.out.print(a[i][n-1]+" ");
}
n--;
/* Print the last row from the remaining rows */
if(k<m)
{
for(i=n-1;i >= l ;--i)
{
System.out.print(a[m-1][i]+" ");
}
m--;
}
/* Print the first column fromthe remaining columns */
if(l<m)
{
for(i = m-1; i>= k; --i)
{
System.out.print(a[i][l]+" ");
}
l++;
}
}
}
}
  3 个评论
John D'Errico
John D'Errico 2016-7-6
It means that we don't write your code for you here. You need to make an effort. After all, this is your homework, not ours.

回答(0 个)

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by