Create a incremented 2D matrix

10 次查看(过去 30 天)
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