How to write this C program in MATLAB?

조회 수: 2 (최근 30일)
Febin Benjamin
Febin Benjamin 2013년 9월 21일
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k,l,m,r;
int a[10][10];
int n=4;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
a[i][j]=1;
}
}
for(i=1;i<=n;i++)
{
m=i;k=i;
for(j=1;j<=i;j++)
{
printf("[%d][%d]\t",m,j);
m--;
}
printf("\n");
}
if(k==n)
{
int p=2;
for(i=n;i>=1;i--)
{
r=k;
for(j=p;j<=n;j++)
{
printf("[%d][%d]\t",r,j);
r--;
}
printf("\n");
p++;
}
}
getch();
}
  댓글 수: 2
Walter Roberson
Walter Roberson 2013년 9월 21일
What is the best way to define "best" ?
dpb
dpb 2013년 9월 21일
cntrOne=0;
cntrTwo=0;
while(cntrOne<=255)
{
i=cntrOne;
while(j<cntrOne)
...
j is undefined here...

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2013년 9월 22일
편집: Image Analyst 2013년 9월 22일
Opening braces { are not used. Closing braces } are "end" in MATLAB. A for loop like
for(j=1;j<=n;j++)
would look like
for j = 1 : n
in MATLAB. printf() is done by fprintf().
p++; would look like p=p+1. Similarly -- would look like p=p-1.
Then you should look over the Getting Started section of the help.
  댓글 수: 1
Febin Benjamin
Febin Benjamin 2013년 9월 22일
for loop syntax was the problem.... Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2013년 9월 21일
  1. cntrTwo does not proceed, such that you get an infinite loop.
  2. j=0 and j++ appear inside the same block, while while(j<cntrOne) is one level higher (see dpb's comment).
  3. if(cntrOne==255) is an exception which occurs in the last iteration only. So better move it behind the outer loop.
My conclusion: The "best" way to convert this pseudo code to Matlab is to fix the problems at first. There is not "best" translation for buggy code.
  댓글 수: 3
Febin Benjamin
Febin Benjamin 2013년 9월 22일
Thank You Jan,dpb & Walter for your comments... I have edited the Question and the description.... :) Check it out.... Walter and Jan, I am extremely sorry for putting 'best' in the question... Seems like you really hung on to that word.... Hehe... :) M quite a rookie actually... Check my edited program... Waiting for your inputs.....
dpb
dpb 2013년 9월 22일
편집: dpb 2013년 9월 24일
I'd suggest rather than posting C-like code expecting someone to read it to decipher intent ask 'how do I compute Whatever?" it is that is the objective end result.
Writing effective Matlab code utilizing the features that make Matlab what it is (namely vectorization thereby avoiding looping constructs in large part and using the builtin functionality to do the work if possible) is not much like writing procedural code in other non-vectorized languages such as Fortran and C, etc., ... at all.
So, what is the end result of the above function intended to be in words and perhaps w/ a small sample input/output? Often such code is reducible in Matlab to just a line or two instead.
For example
int a[10][10];
int n=4;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
a[i][j]=1;
}
}
The above loop (ignoring the fact that C indexing is zero-based so there the indices of a are 0-9 in each dimension even though your pseudo code uses one-based I'll presume that is an attempt to match Matlab) equates in Matlab to
n=4;
a=zeros(10);
a(1:n,1:n)=ones(n);
I've not tried to parse the remainder but undoubtedly it can also be written more succinctly as well.
And, of course, in fact there's no need for a being 10x10 as it seems that only the nxn submatrix is used and the definition is simply there to have static allocation. In Matlab with its dynamic memory management you would in reality just write
n=4;
a=ones(n);
and a "just appears" automagically. If you subsequently change n and rerun, a will be resized to fit.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by