フィルターのクリア

Any way to find which row of a matrix a value is in.

1 回表示 (過去 30 日間)
David
David 2014 年 11 月 14 日
コメント済み: Star Strider 2014 年 11 月 14 日
Hi,
So say I have an AxB matrix which is 100x100 containing data.
I need to perform various calculations on the values based upon which position each value is in.
So is there anyway I can easily find which row of the matrix I am currently in?
i.e. a function within MATLAB that will return which row of the matrix the current variable is in, so if I was currently looking at say a value in row 40, I could do something along the lines of function(matrix()), and it would return the value of 40?
Thanks.
  2 件のコメント
Guillaume
Guillaume 2014 年 11 月 14 日
There is no concept of current variable nor of a variable being in a row, column of a matrix. A variable is just a container for some value.
Similarly which row of the matrix I am currently in doesn't mean anything.
Maybe show a concrete example of what you want to do. For example, starting with
mymatrix = randi(256, 100);
what sort of commands are you planning to use.
David
David 2014 年 11 月 14 日
So I have a matrix that contains data, and I need to multiple each value within the matrix by a constant, yet this constant should vary based upon the current 'y position' in the matrix
i.e. the y position in the matrix represents a radius, and the constant that needs to be mutlipled to each value depends on said radius.
This matrix is created from a set of data loaded in from a separate file, not created within the code itself
Basically:
matrix=load(....)
newmatrix = constant * current y position within matrix * matrix()

サインインしてコメントする。

回答 (2 件)

Star Strider
Star Strider 2014 年 11 月 14 日
The find function (with both row and column outputs) could do what you want.
  2 件のコメント
David
David 2014 年 11 月 14 日
How? I have looked at find and as far as I can tell it will be able to return various positions based upon if the values meet a certain condition (non zero, certain value etc), and not just return what the row and column positions of any given (if not all) value(s).
Star Strider
Star Strider 2014 年 11 月 14 日
When you say ‘matrix’, do you mean ‘vector’? You will likely have to loop through your matrix (or vector) and keep track of where you are as you do your calculations. The find function will tell you the index positions of the values matching the one you are using.

サインインしてコメントする。


Guillaume
Guillaume 2014 年 11 月 14 日
So your constant is not actually constant.
You would have to either
a) use loops to perform your calculation on each column of the matrix
matrix=load(...)
newmatrix = zeros(size(matrix);
for col=1:size(matrix, 2)
newmatrix(:, col) = matrix(:, col) * constant * y;
end
b) create a matrix where your constant is already multiplied by the column and use that to multiply your original value
matrix=load(...)
colmultiplier = constant * (1:size(matrix, 2);
matmultiplier = repmat(constant, size(matrix, 1), 1);
newmatrix = matrix .* matmultiplier;
c) nearly the same as b) but just create a vector of the constant multiplied by the column and use bsxfun to do the multiplication for each row
matrix=load(...)
colmultiplier = constant * (1:size(matrix, 2);
newmatrix = bsxfun(@times, matrix, colmultiplier);

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by