How know the size of matrix after delete raws

What function do you use to know the size of the array after one or more rows are removed after a condition is achieved within a loop

답변 (4개)

dpb
dpb 2018년 6월 22일
편집: dpb 2018년 6월 22일

1 개 추천

"I want to delete all rows that start with an element greater than 5 in the matrix"
In Matlab, use "logical addressing" for such problems...
>> s(s(:,1)>5,:)=[]
s =
2 98 52 41 63
4 5 2 6 9
>>
Shweta Singh
Shweta Singh 2018년 6월 22일

0 개 추천

You can use the 'size' function as follows:
matrix_example = rand(7,8); %matrix of size 7x8
[matrix_x, matrix_y] = size(matrix_example);
% Delete a row
matrix_example(3,:) = [];
% Get the updated dimensions
[matrix_x, matrix_y] = size(matrix_example);
Hope this helps!
asma khaled
asma khaled 2018년 6월 22일

0 개 추천

But if I place this code inside a loop for 1 to the number of rows of matrix, a error message will appear because the size of the array has changed.

댓글 수: 1

That should be a warning and not an error. Can you share your code?

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

asma khaled
asma khaled 2018년 6월 22일

0 개 추천

s=[10 20 30 50 60;80 90 40 10 60;2 98 52 41 63;4 5 2 6 9];
[count,count1]=size(s);
for i=1:count
if (s(i,1)>5)
s(i,:)=[];
[count,count1]=size(s);
end
end
error:Index exceeds matrix dimensions.

댓글 수: 4

Not because of size, though, it's because you've removed too many elements and the loop index i is greater than the number of elements left in the first dimension...
>> s(i,1)
Index exceeds matrix dimensions.
>> i
i =
4
>> size(s)
ans =
3 5
>>
Are trying to actually do something specific here or just learning and "playing around"?
dpb
dpb 2018년 6월 22일
Answer moved to comment -- Asma; use the Comment button to add to Q?, not Answer -- dpb
I want to delete all rows that start with an element greater than 5 in the matrix, s(i,:)=[]; to delete the raw. then the size of the matrix changes so I placed [count,count1]=size(s); But why this code does not work.I do not understand.
dpb
dpb 2018년 6월 22일
편집: dpb 2018년 6월 23일
Because when you got to the loop index with i = 4, you've already removed more than that number of rows so when you write
s(i,1)
where i==4 and there are only three rows left, the array addressing is outside the array dimensions and that causes the error.
To do something like this in a loop, you would want to start at the end of the array and work backwards so that the loop index is decreasing at at least the same rate as the size of the array--
nr=size(s,1); % initial number rows in s
for i=nr:-1:1 % start from end, work to beginning...
if s(i,1)>5, s(i,:)=[]; end % eliminate if desired.
end
By going backwards, you always have at least as many rows left in the array as the index value because you've removed only those of higher initial position previously for the next iteration.
BTW, don't feel badly; this is a general precept that every beginning programmer has had to learn the hard way first... :)
dpb
dpb 2018년 6월 23일
Again, move to Comment...-dpb
Thank you so much for your help

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2018년 6월 22일

댓글:

dpb
2018년 6월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by