Subtracting equal elements of one array from another array of a different size
13 次查看(过去 30 天)
显示 更早的评论
Howdy,
I am having an issue that I would like some help with.
I have two arrays, one is 13x9 and the other is 7x9. What I would like to do is subtract elements of the smaller array third column from the larger array third column given that the elements in the fourth column are equal to each other. I have already reduced the dimensions of the larger array to ensure all elements of the smaller arrays fourth column are present, but I'm getting array dimension mismatch errors.
Something like...
arr1(:,3) - arr2(:,3)
if arr2(:,4) == arr1(:,4)
= newarr(:,:)
Your help is much appreciated!
2 个评论
Matt J
2022-4-22
I recommend providing an example to show what you mean along with the desired output. Perhaps with fewer rows and with only 2 columns (since only columns 3-4 matter for your problem).
采纳的回答
Jeffrey Clark
2022-4-22
One way to allow MATLAB to compare different sized vectors is to have one be a column and the other be a row. Assuming we are still talking about columns 3 and 4 in the 13x9 exmp and 7x9 texmp, you want to change the test for column 4 to be something like this: indexmap = exmp(:,4)==texmp(:,4)'; note the apostrophe changing the texmp column vector to be a row vextor - indexmap will be a 13x7 logical showing in column 1 the indexes of the rows of exmp that need to be modified by the first row data of texmp, and so on for the 2-7 columns. (Note that 2 rows of texmp will not be applied to any of the rows of exmp.)
So then you can loop thru the indexmap columns to apply the subtractions, like this (but you may find a better way now that you have this new tool :)
j = 1;
for i=indexmap;
exmp(i,3) = exmp(i,3)-texmp(j,3);
j = j+1;
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!