필터 지우기
필터 지우기

How to assign proper index within a for loop?

조회 수: 3 (최근 30일)
Ali Y.
Ali Y. 2015년 7월 18일
편집: Azzi Abdelmalek 2015년 7월 18일
Hi, How can I assign proper index within 'for loops', so that I reach to values of all loops? For example, I have a matrix as following; and I want to divide it to two separate matrices. I know I should have two matrices (TA) with dimension: (r=7,c=3) and (r=5,c=3). But I only get the result of last loop , i.e. TA(end). How should I assign proper index at left hand of assignment to get the result of all loops? In this case, I tried TA (c,r,i), so that each matrix get stored in relative (i.e. i) dimension, but it gives an error message "Assignment has more non-singleton rhs dimensions than non-singleton subscripts". Could someone please help me solve and understand this chronic problem of mine?
Thank you in advance
clear all
clc
A1 = [1;1;1;2;2;2;3;3;3;4;4;4];
A2= [1;1;1;1;1;1;1;2;2;2;2;2];
A3 = (10:10:120); A3 = A3';
A = [A1,A2,A3];
b = min(A2): max(A2);
for i = min(b): max(b)
TA = A((find(A2 == b(i))),:);
[r,c] = size(TA)
TA = A((find(A2 == b(i))),:)
end
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 18일
편집: Azzi Abdelmalek 2015년 7월 18일
I tested your code, no error message
Ali Y.
Ali Y. 2015년 7월 18일
Dear Azzi, thank you for your reply, the error arise when the last line of the loop is written as
TA((r,c,i) = A((find(A2 == b(i))),:)
I need to assign TA an index to get the two matrix, when I recall TA outside of the for loop.

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

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 18일
편집: Azzi Abdelmalek 2015년 7월 18일
You can't do
TA((r,c,i) = A((find(A2 == b(i))),:)
Because find(A2 == b(i)) will return different sizes. but you can use cell array
TA{i} = A((find(A2 == b(i))),:)
To get your matrices
TA{1}
TA{2}

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by