필터 지우기
필터 지우기

How to store index values in the form of arrays using for loop?

조회 수: 9 (최근 30일)
SAURAV SANKHE
SAURAV SANKHE 2020년 6월 1일
편집: KALYAN ACHARJYA 2020년 6월 1일
Hello Everyone
Can you please tell me how to save indes values of the following code from the for loop?
rng default
M = zeros(randi([3 6]),randi([3 6]))
[u,v] = size(M)
for i = 1:u
for j = 1:v
ind1(i) = i;
ind2(j) = j;
indices = [ind1(i),ind2(j)]
end
end
I want to store the values in the form of two colums and m*n rows.
Thank you

채택된 답변

Sugar Daddy
Sugar Daddy 2020년 6월 1일
rng default
M = zeros(randi([3 6]),randi([3 6]))
[u,v] = size(M)
inc =1;
for i = 1:u
for j = 1:v
indices(inc,:) = [i ,j];
inc = inc+1;
end
end
or you can use to ind2sub instead of for loop
[x,y]=ind2sub(size(M),1:numel(M));
indices = [y' x'];

추가 답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 6월 1일
편집: KALYAN ACHARJYA 2020년 6월 1일
Cell array
rng default
M = zeros(randi([3 6]),randi([3 6]))
[u,v] = size(M)
indices=cell(u,v);
for i = 1:u
for j = 1:v
ind1(i) = i;
ind2(j) = j;
indices{i,j}=[ind1(i),ind2(j)];
end
end
  댓글 수: 2
SAURAV SANKHE
SAURAV SANKHE 2020년 6월 1일
Thank you Kalyan Acharjya.
Have a great day
madhan ravi
madhan ravi 2020년 6월 1일
You don’t preallocate properly do you?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by