Replacing elements of an array with a vector

조회 수: 1 (최근 30일)
Greggory
Greggory 2011년 5월 31일
편집: Jesús Zambrano 2019년 2월 18일
I know that you can select elements out of a matrix that meet a condition and assign a new value, i.e.
a = [1 2 3 4 5 6 7 9];
a(a>4) = 0;
a
1 2 3 4 0 0 0 0 0
but what if you have a multidimensional matrix? I want to do something similar to the following:
a = [1 2 3 4; 0 0 0 0];
a(a(1,:)==1) = [a b];
a
a 2 3 4
b 0 0 0
The situation is that I have a matrix where the first three dimensions are spatial and then there's a fourth that contains information about the spatial element. Right now, I'm using For loops to check every element and assign a vector [a b c d] to the spatial element. I would like to vectorize this and it's frustrating how easy it is to do this with one scalers, but it doesn't work with vectors.
  댓글 수: 2
Laura Proctor
Laura Proctor 2011년 5월 31일
Could you show what it is your starting with and what you would like to have as the result?
Matt Fig
Matt Fig 2011년 5월 31일
I agree, make a real simple example of your actual data and show what you want to do. I am reading this as you have something like this:
A = round(rand(3,3,3,3)); % Sample data
and you want to find, say, all instances which match this:
V = squeeze(A(2,3,2,:))
along the fourth dimension and replace it with, say:
[4;5;6]
Is this close to what you want?

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

답변 (1개)

Jesús Zambrano
Jesús Zambrano 2019년 2월 18일
편집: Jesús Zambrano 2019년 2월 18일
Hi Greggory,
According to the solution you show, you can get it by doing:
a(:, a(1,:)==1) = [a; b ]
Best regards,
Jesús

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by