Create a incremented 2D matrix

조회 수: 13 (최근 30일)
Midimistro
Midimistro 2016년 4월 12일
댓글: Guillaume 2016년 4월 13일
I am trying to add values to a matrix of 2 dimensions. The rows of toothCount are the values that holds the number of times a specific condition is met. The columns of toothCount are a reference to the offset in which the conidtion is true. I need this to build an array. Here is the working 1D version:
toothCount((find(tmpVec==1)))=toothCount((find(tmpVec==1)))+ones(size(find(tmpVec==1)));
%tmpVec is the condition that must be met in order for that row of toothCount to increment.
How do I go about turning this into a growing 2 dimensional array? This call is within a double for loop, so I want the parent for loop to increment the column and the condition & child for loop take care of the values of the rows (as it is doing in the above 1D version). Is there an easy way to do this?

채택된 답변

Midimistro
Midimistro 2016년 4월 13일
I found the answer. Here's a simplified, better explanation of what I am doing:
A={1.2,1.3,1.6}
B={1.15,1.2,1.22,1.23,1.28,1.29,1.35,1.37,1.59,1.60}
Asize=numel(A);
Bsize=numel(B);
Increment=A;
IncrementInstances= int8(abs(testOffsetStart-testOffsetFinish)/.01);
toothCount = zeros (numel(A),IncrementInstances);
%numel(A) defines number of rows, IncrementInstances defines # of columns.
%toothCount needs to be preallocated beforehand, otherwise you will get a index out of bounds error
toothColumn = 1; % the starting column value of the toothCount matrix
for i = testOffsetStart:.01:testOffsetFinish
{
Increment=A+i;
for j=1:numel(B)
{
if(%Conditional here. Not included for certain reasons)
{
clear tmpVec; %release values from last evaluation;
tempVec=(%insert same conditional here);
toothCount((find(tmpVec==1)), toothColumn)=toothCount((find(tmpVec==1)), toothColumn)+ones(size(find(tmpVec==1)));
toothColumn=toothColumn+1;
%do more stuff here if desired
}
}
}
The above matrix will return a count at each of the given indexes in which A happened to pass the conditional for each offset. If anyone needs a better explanation, please let me know.
In other words, it creates a new column consisting of the 1D toothCount vector, except each column of the toothCount matrix refers to a specific offset.
  댓글 수: 1
Guillaume
Guillaume 2016년 4월 13일
There's no concatenation whatsoever in your answer.
As stated in my answer, the find and the ones expression are completely unnecessary and just slow down the code:
toothCount(tmpVec == 1, toothColumn) = toothCount(tmpVec == 1, toothColumn) + 1;
The clear tmpVec is also completely unnecessary.
Your code is also going to fail if the difference between testOffsetFinish and testOffsetStart is greater than 2.55. You should use proper rounding functions such as floor, round, fix or ceil rather than converting to 8-bit integer with int8.
However, there's still the risk that your calculation of IncrementInstances is off by one compared to the actual number of steps in the for loop due to rounding error (particularly as .01 can't be represented exactly as double). Safer would be to actually generate the iterating vector for the loop and count the number of elements:
Offsets = testOffsetStart : 0.01 : testOffsetFinish;
IncrementInstances = numel(Offsets);
%...
for i = Offsets
%...

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

추가 답변 (1개)

Guillaume
Guillaume 2016년 4월 12일
편집: Guillaume 2016년 4월 12일
A simpler expression for your 1D case would be:
toothCount(tempVec == 1) = toothCount(tempVec == 1) + 1;
Assuming, that tempVec (now tempMat or better a name that actually mean something) is now a matrix the same size and dimension as toothCount the above will work for any dimension.
If not, you need to give more details on what tempVec is.
edit: I'm not sure why you've got a 'concatenation' tag since your example does not involve any concatenation.
  댓글 수: 2
Midimistro
Midimistro 2016년 4월 12일
tmpVec returns the result of a conditional (which is 2 vectors of different size using what's called a combing algorithm), returning a 0 or 1, for a specific set of indexes. ToothCount then adds one to each instance where tempVec is 1.
However, this is not what I am looking for. I am looking to create a 2D array that does the same thing as what I have specified, but adds it as a column of a growing matrix (aka why I added the concatenation tag).
Guillaume
Guillaume 2016년 4월 13일
편집: Guillaume 2016년 4월 13일
"but adds it". What is it?
Can you give a concrete example of what you want?

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by