I have to add the values that are inside the matrix called s

조회 수: 1 (최근 30일)
marie
marie 2012년 12월 2일
s= [...
99 99 0.100 0.120 0 0.500 0;
0 0 0 0 0.150 0.120 0;
0.110 0.010 0.010 0 0 0.300 0.100;
0 0.250 0 0.050 0.060 0.100 0.110;
0 0.120 0.040 0 0.500 0.750 1.200];
I have a file with different matrices. I want to write a general code that will add all the values inside this matrix without including the last two in this case 0.750 1.200 and the 99's
so far I have
r=(s(1:5,1:7))
y=sum(r(r<99))
I don't know to remove the last two values as in a general code that will work for the rest of the matrices.
  댓글 수: 4
Azzi Abdelmalek
Azzi Abdelmalek 2012년 12월 2일
What do you mean by and the 99's
marie
marie 2012년 12월 2일
as in don't include the 99's, remove them

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

채택된 답변

Image Analyst
Image Analyst 2012년 12월 2일
편집: Image Analyst 2012년 12월 2일
Close. But try it this way if you want to sum everything, except for the last two elements in the last row (kind of a weird restriction, but whatever...), that is less than 99. The key is the transpose which you forgot to do, because if you just do (:) it goes down rows in a column first before it moves over to the next column.
clc;
format compact
format longg
s= [...
99 99 0.100 0.120 0 0.500 0;
0 0 0 0 0.150 0.120 0;
0.110 0.010 0.010 0 0 0.300 0.100;
0 0.250 0 0.050 0.060 0.100 0.110;
0 0.120 0.040 0 0.500 0.750 1.200];
sTransposed = s'; % Note the transpose.
% Get all but the last two columns in the last row.
allButLast2 = sTransposed(1:end-2)
% Now threshold at 99 and sum those below the threshold.
y=sum(allButLast2(allButLast2<99))
The answer:
y =
2.75
  댓글 수: 3
marie
marie 2012년 12월 2일
편집: marie 2012년 12월 2일
what if I want to remove the first 5 elements of the matrix s after the transpose?
Image Analyst
Image Analyst 2012년 12월 2일
The first 5 elements after the transpose would be the first 5 rows in column 1 but not including the last two elements in column1 of sTransposed. Remember, MATLAB's linear indexing goes down rows in a column first before moving over to the next column, so the first 5 after transposing is sTransposed(1:5, 1). You can extract them to a new array, but you can't remove (delete/eliminate) them unless you convert sTransposed to a cell array.

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

추가 답변 (0개)

카테고리

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