how matlab reads Column vectors

2 views (last 30 days)
marie lasz
marie lasz on 14 Feb 2021
Commented: marie lasz on 16 Feb 2021
Hello all,
I have a 3 column vector and applying in a if condition. My question is that how matlab reads coulmn vectors? like is it read from rows to rows or is matlab reads column by column?
W_vc_t=[1 1 1];
W_vc=W_vc_t*vector;
if W_vc(k) == 0
b_watermarked{k} = b{k} - alpha;
else
b_watermarked{k} = b{k} + alpha;
end
where vector consists of around 1000 values

Accepted Answer

Image Analyst
Image Analyst on 14 Feb 2021
Let's assume your 3 column vector is something like this:
vector = [1, 2, 3]; % A 3-column "row vector.".
Though it is a 3 column vector with one row, we typically call that a "row vector" since it's only one row with multiple columns.
W_vc = W_vc_t * vector;
would not work because your row vector is on the right side of the other vector.. You'd have
(1 x 3) * (1 x 3)
and the inner dimensions would not match (3 does not match 1). Likewise, this would not work
W_vc = vector * W_vc_t;
because you essentially have
(1 x 3) * (1 x 3)
and the inner dimensions still don't match. I'm sure in your linear algebra class you were taught the trick with holding your fingers horizontal for left matrix and then turning them vertical for the left matrix.
You could do
W_vc = vector .* W_vc_t; % Note the dot before the star.
(1 x 3) .* (1 x 3)
where you you are doing an element-by-element multiplication, not a matrix multiplication. You are doing vector(1) * W_vc_t(1), and vector(2) * W_vc_t(2) and vector(3) * W_vc_t(3) and you'd end up with a 3 element row vector.
It just depends on what you want to do.
  3 Comments
Image Analyst
Image Analyst on 15 Feb 2021
With that line of "if" code, you're using a linear index to a matrix. MATLAB is column-wise and it will return from top left to bottom right, going down rows within a column first, then moving over to the next column to the right until all elements are accessed. Here is a demo:
m = magic(3) % A 3 x 3
for c = 1 : numel(m)
fprintf('m(%d) = %d\n', c, m(c));
end
m =
8 1 6
3 5 7
4 9 2
m(1) = 8
m(2) = 3
m(3) = 4
m(4) = 1
m(5) = 5
m(6) = 9
m(7) = 6
m(8) = 7
m(9) = 2
marie lasz
marie lasz on 16 Feb 2021
thanks I got that. :-)

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!