Filter löschen
Filter löschen

How can I write this C code snippet in MATLAB?

1 Ansicht (letzte 30 Tage)
Febin Benjamin
Febin Benjamin am 5 Jan. 2014
Beantwortet: Adithya am 28 Feb. 2023
for(j=0;j<n;j++)
{
for(i=0,k=j;i<=j && k<=j; i++)
{
printf("%d ", Mat[k][i]);
k--;
}
}
The difference in for loop syntax between the two languages is troubling me actually... Please help
  2 Kommentare
James Tursa
James Tursa am 5 Jan. 2014
What is the line "print [k][i]" supposed to do?
Febin Benjamin
Febin Benjamin am 5 Jan. 2014
Bearbeitet: Febin Benjamin am 5 Jan. 2014
it will print the value present at the point [k][i] of a 2-D matrix

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Adithya
Adithya am 28 Feb. 2023
In the above code you are trying to print elements in anti diagonal fashion for example :
Consider Mat = [1,2,3;4,5,6;7,8,9]
Now u have to print it as
1
4 2
7 5 3
% Define the matrix
m = [1 2 3; 4 5 6; 7 8 9];
% Get the number of rows in the matrix
n = size(m, 1);
% Loop over the rows of the matrix and print them in the desired format
cnt=0;
for j = 1:3
k=1;
for i=1:j
disp(m(j-i+1,k))
k=k+1;
end
end

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by